如何在字符串中分隔第二个逗号

时间:2018-03-15 18:52:05

标签: sql tsql

id  address
-----------------------------------------------
1   hyderabad,madhapur,patrika nagar 500072
2   telangana,uppal, ragive gand nagar,517456
3   usa,verginea,va,123093
4   america,new york
5   india

输出应为:

1     hyderabad,madhapur,
       patrika nagar 500072
2     telangana,uppal, 
       ragive gand nagar,517456
3      usa,verginea,
       123093
4      america,new york
5      india

1 个答案:

答案 0 :(得分:0)

这或许?

DECLARE @string varchar(8000) = 
'id address 1 hyderabad,madhapur,patrika nagar 500072 2 telangana,uppal, ragive gand nagar,517456 3 usa,verginea,va,123093 4 america,new york 5 india'

SELECT newstring = SUBSTRING(@string, d.pos+1, 8000)
FROM (VALUES (CHARINDEX(' ',@string, (CHARINDEX(' ',@string)+1)))) d(pos);

返回

1 hyderabad,madhapur,patrika nagar 500072 2 telangana,uppal, ragive gand nagar,517456 3 usa,verginea,va,123093 4 america,new york 5 india