• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

sql中字符串分割函数

mysql 搞代码 4年前 (2022-01-09) 21次浏览 已收录 0个评论

在sql中没有系统带的的字符分割函数,我们利用了一些功能字了一个分割函数有需要的同学可以参考一下。

来1源gaodai#ma#com搞*代#码1网代码如下复制代码

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/*by kudychen 2011-9-28 */
CREATE function [dbo].[SplitString]
(
@Input nvarchar(max), –input string to be separated
@Separator nvarchar(max)=’,’, –a string that delimit the substrings in the input string
@RemoveEmptyEntries bit=1 –the return value does not include array elements that contain an empty string
)
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

Test 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###’

[Value] from [dbo].[SplitString](@str1, ‘,’, 1)
select [Value] from [dbo].[SplitString](@str2, ‘###’, 1)
select [Value] from [dbo].[SplitString](@str3, ‘###’, 0)

Code:

代码如下复制代码

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/*by kudychen 2011-9-28 */
CREATE function [dbo].[SplitString]
(
@Input nvarchar(max), –input string to be separated
@Separator nvarchar(max)=’,’, –a string that delimit the substrings in the input string
@RemoveEmptyEntries bit=1 –the return value does not include array elements that contain an empty string
)
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

Test 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)


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:sql中字符串分割函数

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址