create function [dbo].[GetSplitOfIndex]
(
@String nvarchar(max), --要分割的字符串
@split nvarchar(10), --分隔符号
@index int --取第几个元素
)
returns nvarchar(1024)
as
begin
declare @location int
declare @start int
declare @next int
declare @seed int
set @String=ltrim(rtrim(@String))
set @start=1
set @next=1
set @seed=len(@split)
set @location=charindex(@split,@String)
while @location<>0 and @index>@next
begin
set @start=@location+@seed
set @location=charindex(@split,@String,@start)
set @next=@next+1
end
if @location =0 select @location =len(@String)+1
return substring(@String,@start,@location-@start)
end
本文介绍了一个SQL函数`[dbo].[GetSplitOfIndex]`,用于根据指定分隔符和位置从字符串中提取子串。该函数在数据库操作中非常实用,特别是在处理分隔的数据时。
8万+

被折叠的 条评论
为什么被折叠?



