This question already has answers here:
How do I split a string so I can access item x?

(44个答案)


6年前关闭。





我有一个关于在T-SQL中拆分列值的问题。
我有

Address_col

Nevada,USA
Tokyo,Japan
Hanoi,Vietnam


我想用逗号分割值

Address_col     Country

Navada          USA
Tokyo           Japan
Hanoi           Vietnam


我怎样才能做到这一点?

最佳答案

declare @T table (Address_col varchar(20))

insert into @T values
('Nevada,USA'),
('Tokyo,Japan'),
('Hanoi,Vietnam')

select left(Address_col, charindex(',', Address_col)-1) as Address_col,
       stuff(Address_col, 1, charindex(',', Address_col), '') as Country
from @T


更新:
将字符串拆分为三个部分可以看起来像这样:
声明@T表(Address_col varchar(20))

insert into @T values
('Nevada,USA,World'),
('Tokyo,Japan,World'),
('Hanoi,Vietnam,World')

select parsename(C, 3),
       parsename(C, 2),
       parsename(C, 1)
from @T
  cross apply (select replace(Address_col, ',', '.')) as T(C)

10-04 19:00