SQL自定义函数split 将数组(分隔字符串)返回阵列(表)
SQL自定义函数split
Create Function Split(@Strs As Nvarchar(1024),@Separator as Nvarchar(10),@Index as Int)
Returns Nvarchar(1024) As
begin
Declare @i As Int, @charpos As Nvarchar(1024)
Set @charpos = @Strs
Set @i = 1
If @Index < 0
Begin
Set @charpos = "超出下界"
End
Else
Begin
While @i <= (@Index - 1)
Begin
If CharIndex(@Separator, @charpos) > 0
Begin
Set @charpos = Substring(@charpos, CharIndex(@Separator, @charpos) + 1, Len(@charpos) - CharIndex(@Separator, @charpos))
End
Else
Begin
Set @charpos = "超出上界"
Break
End
Set @i = @i + 1
End
If @charpos <> "超出上界"
Begin
If CharIndex(@Separator, @charpos) > 0
Begin
Set @charpos = Left(@charpos, CharIndex(@Separator, @charpos) - 1)
End
End
End
Return @charpos
End
--调用
select dbo.Split("sdf|abc|csc|aldsfj|sfj|取出原素|asdf|adf|...","|",6)
--返回 取出原素
Returns Nvarchar(1024) As
begin
Declare @i As Int, @charpos As Nvarchar(1024)
Set @charpos = @Strs
Set @i = 1
If @Index < 0
Begin
Set @charpos = "超出下界"
End
Else
Begin
While @i <= (@Index - 1)
Begin
If CharIndex(@Separator, @charpos) > 0
Begin
Set @charpos = Substring(@charpos, CharIndex(@Separator, @charpos) + 1, Len(@charpos) - CharIndex(@Separator, @charpos))
End
Else
Begin
Set @charpos = "超出上界"
Break
End
Set @i = @i + 1
End
If @charpos <> "超出上界"
Begin
If CharIndex(@Separator, @charpos) > 0
Begin
Set @charpos = Left(@charpos, CharIndex(@Separator, @charpos) - 1)
End
End
End
Return @charpos
End
--调用
select dbo.Split("sdf|abc|csc|aldsfj|sfj|取出原素|asdf|adf|...","|",6)
--返回 取出原素
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了
copyright © 2008-2019 入门客AI创业平台 版权所有 备案号:湘ICP备2023012770号
