日期:2014-05-17 浏览次数:20424 次
Begin try
drop table #test
End try
Begin Catch
print '不存在#test'
End Catch
CREATE TABLE #test
(
ID UNIQUEIDENTIFIER,
NAME NVARCHAR(max),
SORTNO int
)
INSERT INTO #test
( ID, NAME, SORTNO )
VALUES ( NEWID(), -- ID - uniqueidentifier
N'aa,bb,cc,dd349', -- NAME - nvarchar(50)
1 -- SORTNO - int
)
INSERT INTO #test
( ID, NAME, SORTNO )
VALUES ( NEWID(), -- ID - uniqueidentifier
N'qqq,www,eee', -- NAME - nvarchar(50)
3 -- SORTNO - int
)
go
select * from #test ---原表
go
select id,
--name转换后的表,
SUBSTRING(t.name, number ,CHARINDEX(',',t.name+',',number)-number) as name,
SORTNO
from #test t,master..spt_values s
where s.number >=1
and s.type = 'P'
and SUBSTRING(','+t.name,s.number,1) = ','
go
select number from master..spt_values s where s.number >=1 ---系统数字列 1---2047
and s.type = 'P'
select CHARINDEX (',',name,0) from #test ---获取第一个 逗号位置
select SUBSTRING (name,0, CHARINDEX (',',name,0)) from #test ---获取第一个逗号前面的值
select CHARINDEX (',',name,CHARINDEX (',',name,0)+1) from #test ---获取第二个 逗号位置
select CHARINDEX (',',name,CHARINDEX (',',name,CHARINDEX (',',name,0)+1)+1) from #test ---获取第三个 逗号位置