当前位置: 代码迷 >> Sql Server >> 简单有关问题:怎么分别取出ABC来,为据为:(a,b,c)
  详细解决方案

简单有关问题:怎么分别取出ABC来,为据为:(a,b,c)

热度:74   发布时间:2016-04-27 19:10:31.0
简单问题:如何分别取出ABC来,为据为:(a,b,c)
我现在有一字段值为:a,b,c
分隔符为逗号.
我现在想实现目地值为:
字段:
id name
1 a
2 b
3 c

在SQL中咋实现呢?

------解决方案--------------------
SQL code
--折分数组中的元素,并计算当前元素在数组中的位置posCREATE 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');GOselect identity(int,1,1) as n into #temp1 from syscolumnsSELECT arrid, n - LEN(REPLACE(LEFT(array, n), ',', '')) + 1 AS pos, SUBSTRING(array, n, CHARINDEX(',', array + ',', n) - n) AS elementFROM dbo.Arrays  JOIN dbo.#temp1    ON n <= LEN(array)    AND SUBSTRING(',' + array, n, 1) = ',';结果:arrid  pos     elementA    1    20A    2    22A    3    25A    4    25A    5    14B    1    30B    2    33B    3    28C    1    12C    2    10C    3    8C    4    12C    5    12C    6    13C    7    12C    8    14C    9    10C    10    9D    1    -4D    2    -6D    3    -4D    4    -2
------解决方案--------------------
SQL code
拆分表:--> --> (Roy)生成測試數據 if not object_id('Tab') is null    drop table TabGoCreate table Tab([Col1] int,[COl2] nvarchar(5))Insert Tabselect 1,N'a,b,c' union allselect 2,N'd,e' union allselect 3,N'f'GoSQL2000用辅助表:if object_id('Tempdb..#Num') is not null    drop table #Numgoselect top 100 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns bSelect     a.Col1,COl2=substring(a.Col2,b.ID,charindex(',',a.Col2+',',b.ID)-b.ID) from     Tab a,#Num bwhere    charindex(',',','+a.Col2,b.ID)=b.ID --也可用 substring(','+a.COl2,b.ID,1)=','SQL2005用Xml:select     a.COl1,b.Col2from     (select Col1,COl2=convert(xml,'<root><v>'+replace(COl2,',','</v><v>')+'</v></root>') from Tab)aouter apply    (select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/root/v')C(v))bSQL05用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 Tabunion allselect 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           a1           b1           c2           d2           e3           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)
  相关解决方案