rextester:http://rextester.com/ZCU48506
功能:杰夫MODEN的分裂N4k
create function dbo.DelimitedSplitN4K (
@pString nvarchar(4000)
, @pDelimiter nchar(1)
)
returns table with schemabinding as
return
with e1(n) as (
select 1 union all select 1 union all select 1 union all
select 1 union all select 1 union all select 1 union all
select 1 union all select 1 union all select 1 union all select 1
)
, e2(n) as (select 1 from e1 a, e1 b)
, e4(n) as (select 1 from e2 a, e2 b)
, cteTally(n) as (select top (isnull(datalength(@pString)/2,0))
row_number() over (order by (select null)) from e4)
, cteStart(n1) as (select 1 union all
select t.n+1 from cteTally t where substring(@pString,t.n,1) = @pDelimiter)
, cteLen(n1,l1) as(select s.n1
, isnull(nullif(charindex(@pDelimiter,@pString,s.n1),0)-s.n1,4000)
from cteStart s
)
select ItemNumber = row_number() over(order by l.n1)
, Item = substring(@pString, l.n1, l.l1)
from cteLen l;
go
查询:
declare @sample nvarchar (64) = '123,213,312,231'
select * from dbo.DelimitedSplitN4K(@sample,',')
结果
+------------+------+
| ItemNumber | Item |
+------------+------+
| 1 | 123 |
| 2 | 213 |
| 3 | 312 |
| 4 | 231 |
+------------+------+
分割字符串参考:
哇,这看起来不可思议。非常感谢链接这个自定义工具!将标记为答案 –