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
目录
相关文章
|
14天前
|
SQL 索引
在 SQL Server 中使用 STRING_AGG 函数
【8月更文挑战第5天】
165 2
在 SQL Server 中使用 STRING_AGG 函数
|
4天前
|
SQL Java 测试技术
3、Mybatis-Plus 自定义sql语句
这篇文章介绍了如何在Mybatis-Plus框架中使用自定义SQL语句进行数据库操作。内容包括文档结构、编写mapper文件、mapper.xml文件的解释说明、在mapper接口中定义方法、在mapper.xml文件中实现接口方法的SQL语句,以及如何在单元测试中测试自定义的SQL语句,并展示了测试结果。
3、Mybatis-Plus 自定义sql语句
|
12天前
|
SQL 数据库
|
14天前
|
SQL XML JSON
在 SQL Server 中使用字符串转义
【8月更文挑战第5天】
38 7
在 SQL Server 中使用字符串转义
|
14天前
|
SQL 缓存 BI
在 SQL Server 中使用 SPACE 函数
【8月更文挑战第5天】
40 6
在 SQL Server 中使用 SPACE 函数
|
11天前
|
SQL 数据采集 数据处理
如何在 SQL Server 中使用 LEN 函数
【8月更文挑战第9天】
33 1
如何在 SQL Server 中使用 LEN 函数
|
14天前
|
SQL 数据格式
在 SQL Server 中使用 STR 函数
【8月更文挑战第5天】
63 3
在 SQL Server 中使用 STR 函数
|
12天前
|
SQL 监控 索引
如何在 SQL Server 中使用 `PATINDEX` 函数
【8月更文挑战第8天】
87 9
|
12天前
|
SQL 关系型数据库 MySQL
如何在 SQL Server 中使用 `REPLACE` 函数
【8月更文挑战第8天】
110 9
|
13天前
|
SQL 存储 关系型数据库
SQL字符串查询有哪些坑?
本文通过创建一个包含不同格式姓名数据的表格,探讨了MySQL中字符排序规则(Collation)的影响。通过使用不区分大小写和空格的查询条件,文章演示了如何获取所有插入的记录,并解释了排序规则中&quot;_ci&quot;、&quot;_cs&quot;及&quot;_bin&quot;的区别。此外,还强调了在数据处理过程中,应考虑大小写敏感性和字符串前后空格的问题,以防导致统计或比较上的错误。最后,提供了Go语言中处理这类问题的方法,如使用`strings.EqualFold()`进行不区分大小写的字符串比较,以及使用`strings.TrimSpace()`去除字符串两端的空白字符。