SQL Server parses string table value functions

Source: Internet
Author: User

In databases, strings in certain formats are often used for parsing. I have previously written a similar method, and today I have written another one, and I will share it with you.

Create Function [DBO]. [func_splitstringtotable] (-- string to be parsed @ STR nvarchar (max), -- separator @ split varchar (10) returns @ t table (C1 varchar (100 )) asbegin declare @ I int declare @ s int set @ I = 1 Set @ s = 1 while (@ I> 0) Begin set @ I = charindex (@ split, @ STR, @ s) if (@ I> 0) Begin insert @ T (C1) values (substring (@ STR, @ s, @ I-@ s )) end else begin insert @ T (C1) values (substring (@ STR, @ s, Len (@ Str)-@ s + 1 )) end set @ s = @ I + 1 end return end

The usage is as follows:

Select * From DBO. func_splitstringtotable ('1, 15,13 ',',');

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.