日期:2014-05-18  浏览次数:20744 次

简单问题:如何分别取出ABC来,为据为:(a,b,c)
我现在有一字段值为:a,b,c
分隔符为逗号.
我现在想实现目地值为:
字段:
id name
1 a
2 b
3 c

在SQL中咋实现呢?

------解决方案--------------------
SQL code
--折分数组中的元素,并计算当前元素在数组中的位置pos
CREATE TABLE dbo.Arrays
(
  arrid VARCHAR(10)   NOT NULL PRIMARY KEY,
  array VARCHAR(8000) NOT NULL
)

INSERT INTO Arrays(arrid, array) VALUES('A', '20,22,25,25,14');
INSERT INTO Arrays(arrid, array) VALUES('B', '30,33,28');
INSERT INTO Arrays(arrid, array) VALUES('C', '12,10,8,12,12,13,12,14,10,9');
INSERT INTO Arrays(arrid, array) VALUES('D', '-4,-6,-4,-2');
GO
select identity(int,1,1) as n into #temp1 from syscolumns

SELECT arrid, n - LEN(REPLACE(LEFT(array, n), ',', '')) + 1 AS pos, SUBSTRING(array, n, CHARINDEX(',', array + ',', n) - n) AS element
FROM dbo.Arrays
  JOIN dbo.#temp1
    ON n <= LEN(array)
    AND SUBSTRING(',' + array, n, 1) = ',';

结果:
arrid  pos     element
A    1    20
A    2    22
A    3    25
A    4    25
A    5    14
B    1    30
B    2    33
B    3    28
C    1    12
C    2    10
C    3    8
C    4    12
C    5    12
C    6    13
C    7    12
C    8    14
C    9    10
C    10    9
D    1    -4
D    2    -6
D    3    -4
D    4    -2

------解决方案--------------------
SQL code
拆分表:

--> --> (Roy)生成測試數據
 
if not object_id('Tab') is null
    drop table Tab
Go
Create table Tab([Col1] int,[COl2] nvarchar(5))
Insert Tab
select 1,N'a,b,c' union all
select 2,N'd,e' union all
select 3,N'f'
Go

SQL2000用辅助表:
if object_id('Tempdb..#Num') is not null
    drop table #Num
go
select top 100 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns b
Select 
    a.Col1,COl2=substring(a.Col2,b.ID,charindex(',',a.Col2+',',b.ID)-b.ID) 
from 
    Tab a,#Num b
where
    charindex(',',','+a.Col2,b.ID)=b.ID --也可用 substring(','+a.COl2,b.ID,1)=','


SQL2005用Xml:

select 
    a.COl1,b.Col2
from 
    (select Col1,COl2=convert(xml,'<root><v>'+replace(COl2,',','</v><v>')+'</v></root>') from Tab)a
outer apply
    (select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/root/v')C(v))b




SQL05用CTE:

;with roy as 
(select Col1,COl2=cast(left(Col2,charindex(',',Col2+',')-1) as nvarchar(100)),Split=cast(stuff(COl2+',',1,charindex(',',Col2+','),'') as nvarchar(100)) from Tab
union all
select Col1,COl2=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)) from Roy where split>''
)
select COl1,COl2 from roy order by COl1 option (MAXRECURSION 0)

生成结果:
/*
Col1        COl2
----------- -----
1           a
1           b
1           c
2           d
2           e
3           f
*/

------解决方案--------------------
SQL code
if object_id('[tb]') is not null drop table [tb] 
 go 
create table [tb]([id] int,[name] varchar(10))
insert [tb] select 1,'a'
union all select 2,'b'
union all select 3,'c'


select STUFF((select ','+name from tb for xml path('')),1,1,'')
/*
------------
a,b,c

(1 行受影响)
*/

------解决方案--------------------
把长度设的大一些。看红字部分,上面的只是举个例子
declare @str varchar(8000) 

set @str = 'a1,b1,c2,d1,e3,f5' 

set @str = 'select name='''+replace(@str,',',''''+' union all select ''')+'''' 


print @str
exec (@str)