SQLSplit split string

Source: Internet
Author: User
SQL Server splits the string to a temporary table without GOCreatefunction [dbo]. [split] (@ SourceSqlvarchar (max), @ StrSepratevarchar (10) returns @ temptable (linevarchar (max) asbegindeclare @ iintset @ SourceSqlrtrim (ltrim (@ SourceSql )) set @ icharindex (@ StrSep

SQL Server Splits a string to a temporary table without the GOCreate function [dbo]. [split] (@ SourceSql varchar (max), @ StrSeprate varchar (10) returns @ temp table (line varchar (max )) asbegin declare @ I int set @ SourceSql = rtrim (ltrim (@ SourceSql) set @ I = charindex (@ StrSep

SQL Server splits strings into temporary tables <无>
GOCreate function [dbo]. [split] (@ SourceSql varchar (max), @ StrSeprate varchar (10) returns @ temp table (line varchar (max )) asbegin declare @ I int set @ SourceSql = rtrim (ltrim (@ SourceSql) set @ I = charindex (@ StrSeprate, @ SourceSql) while @ I> = 1 begin if len (left (@ SourceSql, @ i-1)> 0 begin insert @ temp values (left (@ SourceSql, @ i-1 )) end set @ SourceSql = substring (@ SourceSql, @ I + len (@ StrSeprate), len (@ SourceSql)-@ I) set @ I = charindex (@ StrSeprate, @ SourceSql) end if @ SourceSql <> ''insert @ temp values (@ SourceSql) returnend ----- test GOSelect * From dbo. split ('asd, f, gh, jk, L ',',')
-- SQL recursive Create function [dbo]. [split] (@ SourceSql varchar (max), @ StrSeprate varchar (10) returns @ temp table (line varchar (max) asbegin; with roy as (select COl2 = cast (substring (@ SourceSql + @ StrSeprate, 1, charindex (@ StrSeprate, @ SourceSql + @ StrSeprate)-1) as nvarchar (max )), split = cast (substring (@ SourceSql + @ StrSeprate, charindex (@ StrSeprate, @ SourceSql + @ StrSeprate) + 1, len (@ SourceSql + @ StrSeprate)-charindex (@ StrSeprate, @ SourceSql + @ StrSeprate) as varchar (max) union allselect COl2 = cast (substring (Split, 1, charindex (@ StrSeprate, Split)-1) as nvarchar (100) as nvarchar (max), Split = cast (substring (Split, charindex (@ StrSeprate, Split) + 1, len (Split) -charindex (@ StrSeprate, Split) as varchar (max) from Roy where charindex (@ StrSeprate, Split)> 1) Insert into @ temp (line) select COl2 from roy option (MAXRECURSION 0) 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.