The SQL Server String splitting (split) method is used to collect and learn carefully)

Source: Internet
Author: User

 

SQL Server String splitting (split) method summary

-- Method 0: dynamic SQL
Declare @ s varchar (100), @ SQL varchar (1000)
Set @ s = '1, 2, 3, 4, 5, 6, 7, 8, 9, 10'
Set @ SQL = 'select Col = ''' + Replace (@ s, ',', ''' Union all select ''') + ''''
Print @ SQL
Exec (@ SQL)

 

If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [f_splitstr] ') and xtype in (n'fn', n'if', n'tf '))
Drop function [DBO]. [f_splitstr]
Go
-- Method 1: Circular Truncation
Create Function f_splitstr (
@ S varchar (8000), -- string to be split
@ Split varchar (10) -- Data Separator
) Returns @ Re table (COL varchar (100 ))
As
Begin
Declare @ splitlen int
Set @ splitlen = Len (@ split + 'A')-2
While charindex (@ split, @ s)> 0
Begin
Insert @ Re values (left (@ s, charindex (@ split, @ s)-1 ))
Set @ s = stuff (@ s, 1, charindex (@ split, @ s) + @ splitlen ,'')
End
Insert @ Re values (@ s)
Return
End
Go

 

If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [f_splitstr] ') and xtype in (n'fn', n'if', n'tf '))
Drop function [DBO]. [f_splitstr]
Go
-- Method 2: Use the temporary partitioning auxiliary Table Method
Create Function f_splitstr (
@ S varchar (8000), -- string to be split
@ Split varchar (10) -- Data Separator
) Returns @ Re table (COL varchar (100 ))
As
Begin
-- Create a secondary table for splitting (only table variables can be operated in user-defined functions)
Declare @ t table (ID int identity, B bit)
Insert @ T (B) Select top 8000 0 from syscolumns A, syscolumns B

Insert @ Re select substring (@ s, ID, charindex (@ split, @ s + @ split, ID)-ID)
From @ t
Where id <= Len (@ s + 'A ')
And charindex (@ split, @ split + @ s, ID) = ID
Return
End
Go

 

If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [f_splitstr] ') and xtype in (n'fn', n'if', n'tf '))
Drop function [DBO]. [f_splitstr]
Go
If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [tb_splitstr] ') and objectproperty (ID, n' isusertable') = 1)
Drop table [DBO]. [tb_splitstr]
Go
-- Method 3: Permanent partitioning of auxiliary tables
-- String splitting auxiliary table
Select top 8000 id = identity (INT,) into DBO. tb_splitstr
From syscolumns A, syscolumns B
Go
-- String splitting Handler
Create Function f_splitstr (
@ S varchar (8000), -- string to be split
@ Split varchar (10) -- Data Separator
) Returns table
As
Return (
Select Col = cast (substring (@ s, ID, charindex (@ split, @ s + @ split, ID)-ID) as varchar (100 ))
From tb_splitstr
Where id <= Len (@ s + 'A ')
And charindex (@ split, @ split + @ s, ID) = ID)
Go

 

-- Method 4: use SQL server2005 outer apply

Create Function [DBO]. [ufn_splitstringtotable]
(
@ STR varchar (max ),
@ Split varchar (10)
)
Returns table
As
Return
(Select B. ID
From (select [value] = convert (XML, '<v>' + Replace (@ STR, @ split, '</V> <v> ')
+ '</V> ')
)
Outer apply (select id = n. v. Value ('.', 'varchar (100 )')
From a. [value]. nodes ('/V') N (v)
) B
)

 

Note:

Method 4 can be run only in SQL server2005

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.