a1 a2
565 123;456;789
拆分
a1 a2
565 123
565 456
565 789
4 个解决方案
#1
baidu搜:sql server拆分字符串
例如:http://www.cnblogs.com/aierong/archive/2008/11/19/sqlserver_split.html
例如:http://www.cnblogs.com/aierong/archive/2008/11/19/sqlserver_split.html
#2
;WITH t(a1,a2) AS (
select 565 ,'123;456;789'
)
SELECT a.a1,b.v FROM (
SELECT t.a1,CONVERT(XML,'<n>'+REPLACE(t.a2,';','</n><n>')+'</n>') as x FROM t
) a CROSS APPLY(SELECT k.n.value('.','int') v FROM a.x.nodes('n') k(n)) b
/*
a1 v
565 123
565 456
565 789
*/
#3
splite函数
#4
或用
use Tempdb
go
--> --> 中国风(Roy)生成測試數據
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([a1] int,[a2] nvarchar(31))
Insert #T
select 565,N'123;456;789'
Go
SELECT a.[a1] ,
[a2] = SUBSTRING(a.[a2], b.number,
CHARINDEX(';', [a2] + ';', b.number) - b.number)
FROM #T AS a ,
master.dbo.spt_values AS b
WHERE b.type = 'P'
AND CHARINDEX(';', ';' + [a2], b.number) = b.number;
/*
a1 a2
565 123
565 456
565 789
*/
#1
baidu搜:sql server拆分字符串
例如:http://www.cnblogs.com/aierong/archive/2008/11/19/sqlserver_split.html
例如:http://www.cnblogs.com/aierong/archive/2008/11/19/sqlserver_split.html
#2
;WITH t(a1,a2) AS (
select 565 ,'123;456;789'
)
SELECT a.a1,b.v FROM (
SELECT t.a1,CONVERT(XML,'<n>'+REPLACE(t.a2,';','</n><n>')+'</n>') as x FROM t
) a CROSS APPLY(SELECT k.n.value('.','int') v FROM a.x.nodes('n') k(n)) b
/*
a1 v
565 123
565 456
565 789
*/
#3
splite函数
#4
或用
use Tempdb
go
--> --> 中国风(Roy)生成測試數據
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([a1] int,[a2] nvarchar(31))
Insert #T
select 565,N'123;456;789'
Go
SELECT a.[a1] ,
[a2] = SUBSTRING(a.[a2], b.number,
CHARINDEX(';', [a2] + ';', b.number) - b.number)
FROM #T AS a ,
master.dbo.spt_values AS b
WHERE b.type = 'P'
AND CHARINDEX(';', ';' + [a2], b.number) = b.number;
/*
a1 a2
565 123
565 456
565 789
*/