SQL里面用自定义Split()完成个性化需求
2022-11-12 09:43:44
内容摘要
这篇文章主要为大家详细介绍了SQL里面用自定义Split()完成个性化需求,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
代码如下:
SET A
文章正文
这篇文章主要为大家详细介绍了SQL里面用自定义Split()完成个性化需求,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <code> SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE function [dbo].[SplitString] ( @Input nvarchar(max), @Separator nvarchar(max)= ',' , @RemoveEmptyEntries bit=1 ) returns @TABLE table ( [Id] int identity(1,1), [Value] nvarchar(max) ) as begin declare @Index int, @Entry nvarchar(max) set @Index = charindex(@Separator,@Input) while (@Index>0) begin set @Entry=ltrim(rtrim(substring(@Input, 1, @Index-1))) if (@RemoveEmptyEntries=0) or (@RemoveEmptyEntries=1 and @Entry<> '' ) begin insert into @TABLE([Value]) Values(@Entry) end set @Input = substring(@Input, @Index+datalength(@Separator)/2, len(@Input)) set @Index = charindex(@Separator, @Input) end set @Entry=ltrim(rtrim(@Input)) if (@RemoveEmptyEntries=0) or (@RemoveEmptyEntries=1 and @Entry<> '' ) begin insert into @TABLE([Value]) Values(@Entry) end return end </code> |
代码如下:
1 2 3 4 5 6 7 8 9 | <code> declare @str1 varchar(max), @str2 varchar(max), @str3 varchar(max) set @str1 = '1,2,3' set @str2 = '1###2###3' set @str3 = '1###2###3###' select [Value] from [dbo].[SplitString](@str1, ',' , 1) select [Value] from [dbo].[SplitString](@str2, '###' , 1) select [Value] from [dbo].[SplitString](@str3, '###' , 0) </code> |
注:关于SQL里面用自定义Split()完成个性化需求的内容就先介绍到这里,更多相关文章的可以留意
代码注释