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

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
简介: 原文:SQL Server 自定义字符串分割函数一、按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果(标量值函数)   1 create function Func_StrArrayLength 2 ( ...
原文: 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  
相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
MyBatis动态SQL字符串空值判断,这个细节99%的程序员都踩过坑!
本文深入探讨了MyBatis动态SQL中字符串参数判空的常见问题。通过具体案例分析,对比了`name != null and name != &#39;&#39;`与`name != null and name != &#39; &#39;`两种写法的差异,指出后者可能引发逻辑混乱。为避免此类问题,建议在后端对参数进行预处理(如trim去空格),简化MyBatis判断逻辑,提升代码健壮性与可维护性。细节决定成败,严谨处理参数判空是写出高质量代码的关键。
294 0
3、Mybatis-Plus 自定义sql语句
这篇文章介绍了如何在Mybatis-Plus框架中使用自定义SQL语句进行数据库操作。内容包括文档结构、编写mapper文件、mapper.xml文件的解释说明、在mapper接口中定义方法、在mapper.xml文件中实现接口方法的SQL语句,以及如何在单元测试中测试自定义的SQL语句,并展示了测试结果。
3、Mybatis-Plus 自定义sql语句
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
本文详细介绍了MyBatis的各种常见用法MyBatis多级缓存、逆向工程、分页插件 包括获取参数值和结果的各种情况、自定义映射resultMap、动态SQL
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
ClkLog埋点分析系统支持自定义SQL 查询
本期主要为大家介绍ClkLog九月上线的新功能-自定义SQL查询。
ClkLog埋点分析系统支持自定义SQL 查询
|
11月前
|
|
11月前
|
在 SQL Server 中使用字符串转义
【8月更文挑战第5天】
938 7
在 SQL Server 中使用字符串转义
SQL语句中的引号使用技巧:正确处理字符串与标识符
在编写SQL语句时,引号的使用是一个基础且重要的环节
1264 0
|
11月前
|
如何在 SQL Server 中使用 `PATINDEX` 函数
【8月更文挑战第8天】
896 9
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问