select @s = N',,45 , 1, 56, 346 456,8,5, ,d,1,4, 5 9 ,t,,4,5 ,,, w, 3,,'
select * from dbo.split(@s,1,0)
CREATE FUNCTION SPLIT
(
@s nvarchar(max),
@trimPieces bit,
@returnEmptyStrings bit
)
returns @t table (val nvarchar(max))
as
begin
declare @i int, @j int
select @i = 0, @j = (len(@s) - len(replace(@s,',','')))
;with cte
as
(
select
i = @i + 1,
s = @s,
n = substring(@s, 0, charindex(',', @s)),
m = substring(@s, charindex(',', @s)+1, len(@s) - charindex(',', @s))
union all
select
i = cte.i + 1,
s = cte.m,
n = substring(cte.m, 0, charindex(',', cte.m)),
m = substring(
cte.m,
charindex(',', cte.m) + 1,
len(cte.m)-charindex(',', cte.m)
)
from cte
where i <= @j
)
insert into @t (val)
select pieces
from
(
select
case
when @trimPieces = 1
then ltrim(rtrim(case when i <= @j then n else m end))
else case when i <= @j then n else m end
end as pieces
from cte
) t
where
(@returnEmptyStrings = 0 and len(pieces) > 0)
or (@returnEmptyStrings = 1)
option (maxrecursion 0)
return
end
GO
浙公网安备 33010602011771号