您肯定会从中得到一些帮助: -
CREATE FUNCTION CaseSensitiveSQLSplitFunction
(
@str nvarchar(max)
)
returns @t table (val nvarchar(max))
as
begin
declare @i int, @j int
select @i = 1, @j = len(@str)
declare @w nvarchar(max)
while @i <= @j
begin
if substring(@str,@i,1) = UPPER(substring(@str,@i,1)) collate Latin1_General_CS_AS
begin
if @w is not null
insert into @t (val) select @w
set @w = substring(@str,@i,1)
end
else
set @w = @w + substring(@str,@i,1)
set @i = @i + 1
end
if @w is not null
insert into @t (val) select @w
return
end
以样本为:-
declare @str nvarchar(max) = N'ThisIsATest'
select * from dbo.CaseSensitiveSQLSplitFunction(@str)
set @str = N'ThisIsASqlServerCaseSensitiveSplitStringFunction'
select * from dbo.CaseSensitiveSQLSplitFunction(@str)
现在可以以从行到单列值的方式对字符串值进行 sql 连接。我们可以使用任何 sql 连接函数。
declare @str nvarchar(max) = N'ThisIsATest'
SELECT LTRIM(STUFF((
SELECT ' ' + val FROM dbo.CaseSensitiveSQLSplitFunction(@str) FOR XML PATH('')
), 1, 1, '')) string
set @str = N'ThisIsASqlServerCaseSensitiveSplitStringFunction'
SELECT LTRIM(STUFF((
SELECT ' ' + val FROM dbo.CaseSensitiveSQLSplitFunction(@str) FOR XML PATH('')
), 1, 1, '')) string