sqlserver2008 拆分字符串。本站提示廣大學習愛好者:(sqlserver2008 拆分字符串)文章只能為提供參考,不一定能成為您想要的結果。以下是sqlserver2008 拆分字符串正文
--解釋:例如,將以下數據
id id_value
-----------------
1 'aa,bb'
2 'aa,bb,cc'
3 'aaa,bbb,ccc'
--轉換成以下的格局
id id_value
-----------------
1 'aa'
1 'bb'
2 'aa'
2 'bb'
2 'cc'
3 'aaa'
3 'bbb'
3 'ccc'
--代碼-------------------------------------------GO
create table ta
(
id int,
id_value varchar(30)
)
insert into ta
select 1,'aa,bb' union all
select 2,'aa,bb,cc' union all
select 3,'aaa,bbb,ccc'
select
top 4000 ix = identity(int, 1, 1)
into #tb
from sys.objects a, sys.objects b
select
id,
id_value = substring(id_value,ix,charindex(',',id_value + ',',ix) - ix)
FROM ta as A, #tb as B
WHERE SUBSTRING( ',' + id_value, ix, 1) = ','
drop table ta
drop table #tb