sql中字符串分割函数

php中文网
发布: 2016-06-07 17:48:55
原创
1489人浏览过

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

 代码如下 复制代码

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

10分钟内自己学会PHP
10分钟内自己学会PHP

10分钟内自己学会PHP其中,第1篇为入门篇,主要包括了解PHP、PHP开发环境搭建、PHP开发基础、PHP流程控制语句、函数、字符串操作、正则表达式、PHP数组、PHP与Web页面交互、日期和时间等内容;第2篇为提高篇,主要包括MySQL数据库设计、PHP操作MySQL数据库、Cookie和Session、图形图像处理技术、文件和目录处理技术、面向对象、PDO数据库抽象层、程序调试与错误处理、A

10分钟内自己学会PHP 524
查看详情 10分钟内自己学会PHP

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)

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号