Splits a string into multiple records according to the specified delimiter, and returns the table's records.

Source: Internet
Author: User
/* Divide the string into multiple records according to the specified delimiter, and return the method of "select * From DBO. f_division_string ('a, BC, DD, E, F, DAF, eee ',', ') */create function dbo. f_division_string (@ STR varchar (4000) -- string to be split, @ separate varchar (100) -- delimiter) returns @ return_table table -- change the number of multi-table values of the auto-increment table, used to return the record set (idx int -- return the index location of the result set, str_name varchar (100) -- split string value) asbegin declare @ count int -- Number of secondary loops, @ separate_len int -- length of the delimiter, @ POS int -- number of times used in the incircle, @ separate_pos int -- set @ STR = ltrim (rtrim (@ Str) set @ separate_len = Len (@ separate) if right (@ STR, @ separate_len) = @ separate begin set @ STR = left (@ STR, Len (@ Str)-@ separate_len) end set @ COUNT = (LEN (@ Str)-len (replace (@ STR, @ separate ,''))) /@ separate_len set @ Pos = 1 while (@ POS <= @ count) -- Insert the in set @ separate_pos = charindex (@ separate, @ Str) insert into @ return_table (idx, str_name) values (@ POs, substring (@ STR, 1, @ separate_pos-1 )) set @ Pos = @ POS + 1 Set @ STR = right (@ STR, Len (@ Str)-@ separate_pos-@ separate_len + 1) -- extract the extracted string from the original string and extract end insert into @ return_table (idx, str_name) -- Insert the last record value values (@ POs, @ Str) returnend

 

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.