Sql-function Create function Database output results are separated by commas, and in development there are also many arguments passed in the form of comma-string arguments (not recommended when data is large).
For example: Find the name "John, Li II" data in the database at this time to do this parameter processing as shown:
The function code is as follows
CREATE FUNCTION [dbo].[ FNSPLITSTR] (@sText NVARCHAR (Max), @sDelim CHAR (1)) RETURNS @retArray TABLE (value VARCHAR) as BEGIN Decla
RE @posStart BIGINT, @posNext BIGINT, @valLen BIGINT, @sValue NVARCHAR (100);
If @sDelim is NULL BEGIN IF LEN (@sText) >100 SET @sText = SUBSTRING (@sText, 1) 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,) INSERT @retAr
Ray (value) VALUES (@sValue); End, end, return,
OK, about the SQL string comma-delimited function here, you can refer to this.