我有一个问题我有一个简单的表,当我选择全部(一列有一些行)时,它看起来像这样。
| a, b, c | - 1st row
| b, d, d | - 2nd row
| d, e, f | - 3rd row
现在尝试用逗号分割这些值,这样每个值都会在单独的行中,如
|a| - 1st row
|b| - 2nd row
|c| - 3rd row
|d| - 4th row
|e| - 5th row
|f| - 6th row
我正在尝试使用类似的东西:
select id,
case when CHARINDEX(', ', [value])>0
then SUBSTRING([value] , 1, CHARINDEX(', ',[value])-1) else [value] end firstname,
CASE WHEN CHARINDEX(', ', [value])>0
THEN SUBSTRING([value],CHARINDEX(', ',[value])+1,len([value])) ELSE NULL END as lastname from table
但不是这样。
答案 0 :(得分:1)
没有UDF解析/拆分功能
您没有指定表名或列名,因此请将YourTable和YourList替换为您的实际表名和列名。
Select Distinct RetVal
,RowNr = Dense_Rank() over (Order by RetVal)
From YourTable A
Cross Apply (
Select RetSeq = Row_Number() over (Order By (Select null))
,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
From (Select x = Cast('<x>'+ replace((Select A.YourList as [*] For XML Path('')),',','</x><x>')+'</x>' as xml).query('.')) as A
Cross Apply x.nodes('x') AS B(i)
) B
<强>返回强>
RetVal RowNr
a 1
b 2
c 3
d 4
e 5
f 6
使用拆分/解析功能(每个人都应该有一个好的)
Select Distinct RetVal
,RowNr = Dense_Rank() over (Order by RetVal)
From YourTable A
Cross Apply (Select * from [dbo].[udf-Str-Parse-8K](A.YourList,',') ) B
UDF - 如果感兴趣
CREATE FUNCTION [dbo].[udf-Str-Parse-8K] (@String varchar(max),@Delimiter varchar(10))
Returns Table
As
Return (
with cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
cte2(N) As (Select Top (IsNull(DataLength(@String),0)) Row_Number() over (Order By (Select NULL)) From (Select N=1 From cte1 a,cte1 b,cte1 c,cte1 d) A ),
cte3(N) As (Select 1 Union All Select t.N+DataLength(@Delimiter) From cte2 t Where Substring(@String,t.N,DataLength(@Delimiter)) = @Delimiter),
cte4(N,L) As (Select S.N,IsNull(NullIf(CharIndex(@Delimiter,@String,s.N),0)-S.N,8000) From cte3 S)
Select RetSeq = Row_Number() over (Order By A.N)
,RetVal = LTrim(RTrim(Substring(@String, A.N, A.L)))
From cte4 A
);
--Much faster than str-Parse, but limited to 8K
--Select * from [dbo].[udf-Str-Parse-8K]('Dog,Cat,House,Car',',')
--Select * from [dbo].[udf-Str-Parse-8K]('John||Cappelletti||was||here','||')
答案 1 :(得分:0)
一种递归cte解决方案,它可以找到字符串中的所有,
,并获得substring
在,
之间的lead
。 (假设您使用的是用于with cte as (
select val,charindex(',',','+val+',') as location from t
union all
select val,charindex(',',','+val+',',location+1) from cte
where charindex(',',','+val+',',location+1) > 0
)
,substrings as (select *,
substring(val,location,
lead(location,1) over(partition by val order by location)-location-1) as sub
from cte)
select distinct sub
from substrings
where sub is not null and sub <> ''
order by 1;
的sql server版本2012+)
cte
1)第一个,
以递归方式获取字符串中的所有,
个位置。 ,
附加在字符串的开头和结尾处,以避免错过,
之前的第一个子字符串和,
之后的最后一个子字符串。
2)对于每个字符串,使用按lead
的位置排序的,
找到下一个wmUtiGKYwMqp0gai7XnDFD4tuZyYHmMKvcDd96ar+3fvuMOeUqBfpcRGuTn7uSXmnxQb4KIkYUryE7Zv5P9xMIhojWVvihzzB1NDae7c0UxOXfFEEdU8uxymGzrs+L3ThVRYoQTv/jGv9UkdvSuUHJEXOjFu7daGb/a9jmOyhGkokX3gUDkd8Kn3sJG51TdfefExGrBApjF/CEnEsLj+lu4fO6+2eLOnb737dW5pfKXN/x3f5JW2XI1zgZIUYPqsiaLz49bETuJT3F8KHZFWfnSyAiwlxY7NinBprOH1V+Fpz1+fL9emJsLieDTub1VgKNCrjcB9jlsGb0qYqowCS3ktewDqeowQbmgDpz3URtOE92O114eCglD2QBrBCcu+zW6nMk27d/gZg67MMKzaRX16F8tvme7oDKkYB6EIPkutkgs/lobR4Zfkl2MH5g4nLxLCj6phHMHDW3Y9TBpmhmtwGWwFfPy7I8QNft+GurGh4DcZvL9zNQfShX6SBN2UaYXZ5uUhFqBQIZLfwxF14URmytajKltrMLmoHZf2KUUwW2pnP0X/jwyQpP9Ose3AxoG6/7CQrnazBmm7c0wyftXAPJ7MIfiRNRNU1+Y1o6SOg4QvXdrSG4cL5hfqjtVyXWHwApKpmkB6KoK5DyapjopmAcRhOEUSPyOYj/QGVYlrwQugIbKU/hLtdzF4Cm5/lzrPYzm6QEr42jEidMDWqrE/sL88XLKlRed//ezwP8+2Fxv+yFrFOGS8pjLw5D6Mw4PIY7KdSnqCNad5TKwNztZbeK8UZrYQdTt6eqzBMSBIhKrWvKA22E+B+Q9fhBgslLr+wcEOP120cHsH8COv5uLGErtk4ojzc0jlT1i/8lOYqe+/vgQ+J48RxuSRvksowx70UtdogECoVpszEMKCsyLiuHbuHv22sC2IjNUv2+JpiifHIZ+UK3ZvXkt9R4d24ojtC7RBUgXBk0eLLMTl1qYZDskg9/ob66t7ptkyNJgnwmk2cAHl/pjbq8wqzdqju9r7Ky0vus8srs0OjZZ7zPEh1oaEoPkkD/uy6smP0yLKQwjCsK/KeaypQH90rlY5ACgogJqmaFX2J3EUDwcXqnGVtHHa8VUy3tIobCrHAn3Wbx0MaKsSJthWTOj91HRqUqJ2t+DPV8XoWfPe6Wz7HoW+8bjkNKU2mth0zxhYvMHFu4fDqlMbG6V/1gfUOihuGc1hI6+IEtvda+gcrXLEITqUs0QYEilGVHJcrQEexps=
的位置。
3)最后得到所有那些非空且不是空字符串的子串。
答案 2 :(得分:0)
您可以使用交叉应用和XML
来完成此操作select distinct
p.a.value('.','varchar(10)') col
from (
select
cast('<x>' + replace(col,', ','</x><x>') + '</x>' as XML) as x
from your_table) t
cross apply x.nodes ('/x') as p(a)
) t