我有一個字符串,它的格式爲99999 124 fdsg
。替換字符串SQL
我想用-
替換第一個空格,下一個空格替換爲非空格。我預期的結果將是這樣的:
99999-124fdsg
要更換我用這個表達的第一個字符:
stuff(Product,charindex('',product),1,'-')as trim
因爲我要來命名新的列作爲裝飾。我該怎麼做第二個角色?
我有一個字符串,它的格式爲99999 124 fdsg
。替換字符串SQL
我想用-
替換第一個空格,下一個空格替換爲非空格。我預期的結果將是這樣的:
99999-124fdsg
要更換我用這個表達的第一個字符:
stuff(Product,charindex('',product),1,'-')as trim
因爲我要來命名新的列作爲裝飾。我該怎麼做第二個角色?
假設,你比如與STUFF
工作正常了第一個空白,你可以用REPLACE
來包裝它。 STUFF
後有只有其他空白左:
replace(stuff(Product,charindex(' ',product),1,'-'),' ','')
工作例如:
DECLARE @s VARCHAR(50) = '99999 124 fdsg';
select replace(stuff(@s,charindex(' ',@s),1,'-'),' ','')
結果
99999-124fdsg
Charindex
有最後一個參數start_location
用它來尋找第二個空字符串
試試這個
DECLARE @str VARCHAR(50) = '99999 124 fdsg'
SELECT Stuff(Stuff(@str,Charindex(' ',@str),1,'-'),Charindex(' ',@str,Charindex(' ',@str) + 1),1,'')
結果:99999-124fdsg
你剛纔不是問這個問題? –