SQL server String comma-separated function sharing, SQL server

Source: Internet
Author: User

SQL server String comma-separated function sharing, SQL server

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.

The function code is as follows:

CREATE FUNCTION [dbo].[fnSplitStr] ( @sText  NVARCHAR(Max), @sDelim  CHAR(1))RETURNS @retArray TABLE ( value VARCHAR(100))ASBEGIN 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 RETURNEND

Now, we will introduce the SQL string comma-separated functions. You can refer to them.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.