数据库字符串截取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 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',',')