SQL Server split Function

Source: Internet
Author: User

The split table function separates a string by a specified delimiter and returns a table.

 
-- The split table function separates a string by a specified delimiter and returns a table. Create Function split (@ string varchar (255), -- string to be split @ separator varchar (255) -- delimiter) returns @ array table (item varchar (255 )) asbegindeclare @ begin int, @ end int, @ item varchar (255) set @ begin = 1 Set @ end = charindex (@ separator, @ string, @ begin) while (@ end <> 0) beginset @ item = substring (@ string, @ begin, @ end-@ begin) insert into @ array (item) values (@ item) set @ begin = @ end + 1 Set @ end = charindex (@ separator, @ string, @ begin) endset @ item = substring (@ string, @ begin, Len (@ string) + 1-@ begin) if (LEN (@ item)> 0) insert into @ array (item) values (substring (@ string, @ begin, Len (@ string) + 1-@ begin) returnend

The split result is modeled after the Java string method split.

Test 1:

Select * From DBO. Split ('a, B, c, D ',',')

Or (a comma is added at the end of the string to be split)

Select * From DBO. Split ('a, B, c, d,',',')

Result:

Test 2:

Select * From DBO. Split (',A,,B, c, D ',',')

Result:

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.