【SQL分享】去除字符串中連續的分隔符【經典】

以下測試用於去除任何字符串中連線的分隔符。

 

--去除字符串中連續的分隔符
declare @str nvarchar(200)
declare @split nvarchar(200),@times int
set @str='中  國1  2 34 55  5 6 7 7';--字符
set @split=' '; --分隔符
select @times=(len(@str)-len(replace(@str,@split+@split,'')))/2
while @times>0
begin
set @str=REPLACE(@str,@split+@split,' ')
set @times=@times-1 
end
select @str


 

希望對大家有作用

測試結果:

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
中 國1 2 34 55 5 6 7 7

(1 行受影響)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章