Sql server 是否有一个SQL函数可以将固定宽度的分隔字符串拆分为一个表,每个表中的每个值都有一行?

Sql server 是否有一个SQL函数可以将固定宽度的分隔字符串拆分为一个表,每个表中的每个值都有一行?,sql-server,tsql,Sql Server,Tsql,我需要能够拆分固定宽度字符串的逗号分隔列表。在一个特定的例子中,我需要拆分一个包含8个字符的十六进制PIN码的列表。其他目标是比SO和Internet上常见的通用SQL拆分函数性能更高的函数,以及用户友好的语法,例如: declare @pinList varchar(max) set @pinList = 'D1D57EFD,9917D94B,0687E581,C6AA229E,044B136B,ED90E4AF,143E23FB,DF5CF1CB,D711F644,67138659' se

我需要能够拆分固定宽度字符串的逗号分隔列表。在一个特定的例子中,我需要拆分一个包含8个字符的十六进制PIN码的列表。其他目标是比SO和Internet上常见的通用SQL拆分函数性能更高的函数,以及用户友好的语法,例如:

declare @pinList varchar(max)
set @pinList = 'D1D57EFD,9917D94B,0687E581,C6AA229E,044B136B,ED90E4AF,143E23FB,DF5CF1CB,D711F644,67138659'

select *
from fixedWidthSplitFunction(@list, 8, ',')

这是一个特殊用途、固定宽度、带分隔符的字符串、表值拆分函数(好的,这是一个很好的例子):

因为它的域是固定宽度的分隔字符串,所以它包含了在大多数通用拆分函数中不可能实现的优化。在我的测试中,它的性能优于我在与物理表连接时遇到的所有其他通用字符串拆分函数,例如:

create function tvfFixedWidthSplitter
(
    @stringList varchar(max),
    @fixedWidth int,
    @delimiter varchar(10)
)
returns @strings table
(
    id int,
    string varchar(max)
)
as

begin

    with buckets as
    (   
        select 1 id

        union all

        select t.id + 1
        from buckets t
        where id = t.id 
            and t.id < len(@stringList)/(@fixedWidth+len(@delimiter))+1
    )

    insert into @strings
    select 
        id, 
        substring(@stringList, ((id - 1) * (@fixedWidth + len(@delimiter)) + (case when len(@delimiter)-1 = 0 then len(@delimiter) else len(@delimiter)-(len(@delimiter)-1) end)), @fixedWidth) string
    from buckets    
    option (maxrecursion 0)

    return;

end
select *
from tvfFixedWidthSplitter('D1D57EFD,9917D94B,0687E581,C6AA229E,044B136B,ED90E4AF,143E23FB,DF5CF1CB,D711F644,67138659', 8, ',')
select u.UserName
from Users u
join tvfFixedWidthSplitter(@pinList, 8, ',') s
    on u.Pin = s.string