原文:SQL Server 自定义字符串分割函数

一、按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果(标量值函数)  
 1 create function Func_StrArrayLength
 2 (
 3   @str varchar(1024),  --要分割的字符串
 4   @split varchar(10)  --分隔符号
 5 )
 6 returns int
 7 as
 8 begin
 9   declare @location int
10   declare @start int
11   declare @length int
12
13   set @str=ltrim(rtrim(@str))
14   set @location=charindex(@split,@str)
15   set @length=1
16   while @location<>0
17   begin
18     set @start=@location+1
19     set @location=charindex(@split,@str,@start)
20     set @length=@length+1
21   end
22   return @length
23 end
24 go

调用示例:select dbo.Func_StrArrayLength('78,1,2,3',',')  
返回值:4  
二、按指定符号分割字符串,返回分割后指定索引的第几个元素,象数组一样方便(标量值函数)
 1 create function Func_StrArrayStrOfIndex
 2 (
 3   @str varchar(1024),  --要分割的字符串
 4   @split varchar(10),  --分隔符号
 5   @index int --取第几个元素
 6 )
 7 returns varchar(1024)
 8 as
 9 begin
10   declare @location int
11   declare @start int
12   declare @next int
13   declare @seed int
14
15   set @str=ltrim(rtrim(@str))
16   set @start=1
17   set @next=1
18   set @seed=len(@split)
19
20   set @location=charindex(@split,@str)
21   while @location<>0 and @index>@next
22   begin
23     set @start=@location+@seed
24     set @location=charindex(@split,@str,@start)
25     set @next=@next+1
26   end
27   if @location =0 select @location =len(@str)+1
28  --这儿存在两种情况:、字符串不存在分隔符号2、字符串中存在分隔符号,跳出while循环后,@location为,那默认为字符串后边有一个分隔符号。
29
30   return substring(@str,@start,@location-@start)
31 end
32 go

调用示例:select dbo.Func_StrArrayStrOfIndex('8,9,4',',',2)  
返回值:9  
三、结合上边两个函数,像数组一样遍历字符串中的元素(表值函数) 
 1 create function Func_SplitStr(@SourceSql varchar(8000), @StrSeprate varchar(100))
 2   returns @temp table(F1 varchar(100))
 3   as
 4   begin
 5   declare @ch as varchar(100)
 6   set @SourceSql=@SourceSql+@StrSeprate
 7   while(@SourceSql<>'')
 8   begin
 9     set @ch=left(@SourceSql,charindex(',',@SourceSql,1)-1)
10     insert @temp values(@ch)
11     set @SourceSql=stuff(@SourceSql,1,charindex(',',@SourceSql,1),'')
12   end
13   return
14   end
15 go

----调用 
select * from dbo.Func_SplitStr('1,2,3,4',',')   
--结果: 
1  
2  
3  

4


另一种方式(表值函数):
 1 create function Func_SplitStr(@str nvarchar(2000),@split nvarchar(2))
 2 returns @t table(AccountCodeID int )
 3 as
 4 begin
 5 declare @tmpAccountCodeID int,@getIndex int
 6 set  @getIndex=charindex(',',@str)
 7 while(@getIndex<>0)
 8 begin
 9     set @tmpAccountCodeID=convert(int,substring(@str,1,@getIndex-1))
10     insert into @t(AccountCodeID) values (@tmpAccountCodeID)
11     set @str=stuff(@str,1,@getIndex,'')
12     set  @getIndex=charindex(',',@str)
13 end
14 insert into @t(AccountCodeID) values (@str)
15 return
16 end
17 go

----调用 
select * from dbo.Func_SplitStr('1,2,3,4',',')   
--结果: 
1  
2  
3  

4

SQL Server 自定义字符串分割函数相关推荐

  1. SQL Server自定义字符串分割函数——Split

    我相信大部分人都碰到过,处理数据的时候,字段的值是以 ',' (逗号)分隔的形式,所以我也不能避免. 然后我才知道,sql 是没有类似于 C# 和 Javascript 这种分割字符串的方法.( Sp ...

  2. SQL Server中字符串处理函数

    SQL Server中截取字符串常用函数 截取字符串中的最后一个斜杠后的所有字符: SELECT A.PIC_URL, RIGHT(A.PIC_URL,(CHARINDEX('/',REVERSE(A ...

  3. 中统计字符串长度的函数_SQL Server中的字符串分割函数

    您是否知道从SQL Server 2016开始,系统就内置STRING_SPLIT函数,该函数用于将字符串分隔的变量拆分为一个可用列表. 对于经常需要分割字符串的技术人员,建议您查看此功能. STRI ...

  4. access 删除字符串中的字符_SQL Server中的字符串分割函数

    您是否知道从SQL Server 2016开始,系统就内置STRING_SPLIT函数,该函数用于将字符串分隔的变量拆分为一个可用列表. 对于经常需要分割字符串的技术人员,建议您查看此功能. STRI ...

  5. SQL Server 分隔字符串函数实现

    SQL Server 分隔字符串函数实现 在SQL Server中有时候也会遇到字符串进行分隔的需求.平时工作中常常遇到这样的需求,例如:人员数据表和人员爱好数据表,一条人员记录可以多多人员爱好记录, ...

  6. sql server 自定义函数的使用

    sql server 自定义函数的使用 自定义函数 用户定义自定义函数像内置函数一样返回标量值,也可以将结果集用表格变量返回 用户自定义函数的类型: 标量函数:返回一个标量值 表格值函数{内联表格值函 ...

  7. SQL Server中的STRING_SPLIT函数

    This article will cover the STRING_SPLIT function in SQL Server including an overview and detailed u ...

  8. SQL Server 自定义快捷键

    SQL Server 自定义快捷键 SQL Server程序员经常要在SSMS(SQL Server Management Studio)或查询分析器(2000以前)中编写T-SQL代码.以下几个技巧 ...

  9. 学习SQL:SQL Server日期和时间函数

    So far, we haven't talked about SQL Server date and time functions. Today we'll change that. We'll t ...

  10. 什么是SQL Server TRIM()函数?

    Hello, readers. In today's article, we will be focusing on SQL Server TRIM() function in detail. 您好, ...

最新文章

  1. 我花了三个小时写了一道题的六千字题解....(POJ 2888 Magic Bracelet)
  2. setFilters使用方法
  3. 十条实用的jQuery代码片段
  4. 【系统设计】架构设计说明书
  5. 敏捷 - #1 原则:早期和持续交付有价值的软件 (#1 Agile Principle)
  6. 苹果被曝寻求收购Drive.ai:吴恩达参与运营,多家中国VC投资
  7. 网络运维常见交换机故障
  8. vs2005下使用ASPNetPage分页的例子1
  9. vue的一些坑(第二天)
  10. jad反编译成java,反编译工具jad的使用(将*.class文件变成*.java文件,附带jad.zip包)...
  11. XAMPP最详细的安装及使用教程
  12. 第九届“图灵杯”NEUQ-ACM程序设计竞赛个人赛 L题 最小生成树
  13. 我在Facebook工作的十大经验分享
  14. 计算机桌面文件删除不掉是怎么了,桌面上文件删不掉_桌面上的压缩文件为什么删除不了?...
  15. 页面打印不全怎么办html css,win7打印网页显示不全怎么办|win7设置网页打印页面的方法...
  16. 项目管理之团队与团队精神
  17. 花样解锁方式:后置、屏下和侧面指纹,你觉得那种最好用
  18. 【原理+实战+视频+源码】抖音,快手大热背后——Android 贴心的音视频学习指南来咯!
  19. Microsoft Office Visio 缺失安装文件的解决方法(附viso安装密钥)
  20. 严格模式、混杂模式与怪异模式

热门文章

  1. wpf 引用的图片文件打包后找不到_PyQT5打包:用PyInstaller遇到的坑
  2. qfile.remove 删除已经被加载的文件_Milvus数据管理:删除的实现原理
  3. oracle 基数 选择率,1.1.2.2 可选择率(1)
  4. 使用Titan Framework搭建一个集群Demo
  5. 学习如何看懂SQL Server执行计划(一)——数据查询篇
  6. 在Spring3中使用注解(@Scheduled)创建计划任务
  7. 光线的方向:顺光、逆光、侧光、侧顺光、侧逆光、顶光、底光
  8. SSH项目ueditor插件jsp版本
  9. Oracle 11g RAC添加一节点过程
  10. HTTP GET请求URL中IP被异常替换的问题