SQL 根据特定分隔符分割字符串


create function [dbo].[GetSplitStr]
(
 @str nvarchar(500),  --要分割的字符串
 @split nvarchar(50),  --分隔符号
 @index int --取第几个元素
)
returns nvarchar(500)
as
begin
 declare @location int
 declare @start int
 declare @next int
 declare @seed int
 set @str=ltrim(rtrim(@str))
 set @start=1
 set @next=1
 set @seed=len(@split)
  
 set @location=charindex(@split,@str)
 while @location<>0 and @index>@next
 begin
   set @start=@location+@seed
   set @location=charindex(@split,@str,@start)
   set @next=@next+1
 end
 if @location =0 select @location =len(@str)+1  
 return substring(@str,@start,@location-@start)
end

执行调用:

select  [dbo].[GetSplitStr]('四川省#S#成都市#S#高新区', '#S#',3)

--结果为 高新区


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM