数据库字符串截取
Create Function [dbo].[Func_SplitStr]
(
@InputStr NVARCHAR(max),
@SplitSymbol NVARCHAR(max) = ','
)
Returns @Func_SplitStr table
(
val nvarchar(max)
)
as
Begin
Declare @str varchar(max)
Declare @substr varchar(max)
Declare @iLen int
Declare @iStart int
set @str=RTrim(Ltrim(@InputStr))
set @iStart=CHARINDEX( @SplitSymbol , @str )
set @iLen=Len( @str )
IF @iStart>0
Begin
set @substr=substring( @str , 1 , @iStart-1 )
set @str=substring( @str , @iStart+1 , @iLen-@iStart )
End
Else
Begin
set @substr=@str
set @str=''
End
set @substr=RTRIM( LTRIM( @substr ) )
insert @Func_SplitStr select id=cast( @substr as nvarchar )
While Len( @str )>0
Begin
---------------- Loop Begin ---------------
set @iStart=CHARINDEX( @SplitSymbol , @str )
set @iLen=Len( @str )
IF @iStart>0
Begin
set @substr=substring( @str , 1 , @iStart-1 )
set @str=substring( @str , @iStart+1 , @iLen-@iStart )
End
Else
Begin
set @substr=@str
set @str=''
End
set @substr=RTRIM( LTRIM( @substr ) )
insert @Func_SplitStr select id=cast( @substr as nvarchar )
---------------- Loop End ----------------
End
Return
End
select * from dbo.Func_SplitStr('a,b,c,d,1,2',',')
浙公网安备 33010602011771号