After SQL-function is created, the output results of the function database are separated by commas (,). During Development, many parameters with comma (,) are passed in as parameters (this is not recommended when data is large)
For example, search for data with the name "Zhang San, Li 'er" and process this parameter in the database.
FunctionCodeAs follows:
View code
Create Function [ DBO ] . [ Fnsplitstr ] (
@ Stext Nvarchar ( Max ),
@ Sdelim Char ( 1 )
)
Returns @ Retarray Table (
Value Varchar ( 100 )
)
As
Begin
Declare
@ Posstart Bigint ,
@ Posnext Bigint ,
@ Vallen Bigint ,
@ Svalue Nvarchar ( 100 );
If @ Sdelim Is Null
Begin
If Len ( @ Stext ) > 100 Set @ Stext = Substring ( @ Stext , 1 , 100 )
Insert @ Retarray (Value)
Values ( @ Stext );
End
Else
Begin
Set @ Posstart = 1 ;
While @ Posstart <= Len ( @ Stext )
Begin
Set @ Posnext = Charindex ( @ Sdelim , @ Stext , @ Posstart );
If @ Posnext <= 0
Set @ Vallen = Len ( @ Stext ) - @ Posstart + 1 ;
Else
Set @ Vallen = @ Posnext - @ Posstart ;
Set @ Svalue = Substring ( @ Stext , @ Posstart , @ Vallen );
Set @ Posstart = @ Posstart + @ Vallen + 1 ;
If Len ( @ Svalue ) > 0
Begin
If Len ( @ Svalue ) > 100 Set @ Svalue = Substring ( @ Svalue , 1 , 100 )
Insert @ Retarray (Value)
Values ( @ Svalue );
End
End
End
Return
End