SQL cut string Split () function

Source: Internet
Author: User
We used php or asp for data cutting. Today, we suddenly found that the Split function in SQL can also be used to cut strings in SQL queries. If you need it, please refer to it.

We used php or asp for data cutting. Today, we suddenly found that the Split function in SQL can also be used to cut strings in SQL queries. If you need it, please refer to it.

Sometimes we use batch operations to Split strings, but SQL Server does not have its own Split function, so we need to implement it ourselves. There's nothing to say. You need to use it directly.

The Code is as follows:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/*
By kudychen 2011-9-28
*/
CREATE function [dbo]. [SplitString]
(
@ Input nvarchar (max), -- input string to be separated
@ Separator nvarchar (max) = ',', -- a string that delimit the substrings in the input string
@ RemoveEmptyEntries bit = 1 -- the return value does not include array elements that contain an empty string
)
Returns @ TABLE table
(
[Id] int identity (1, 1 ),
[Value] nvarchar (max)
)
As
Begin
Declare @ Index int, @ Entry nvarchar (max)
Set @ Index = charindex (@ Separator, @ Input)

While (@ Index> 0)
Begin
Set @ Entry = ltrim (rtrim (substring (@ Input, 1, @ Index-1 )))

If (@ RemoveEmptyEntries = 0) or (@ RemoveEmptyEntries = 1 and @ Entry <> '')
Begin
Insert into @ TABLE ([Value]) Values (@ Entry)
End

Set @ Input = substring (@ Input, @ Index + datalength (@ Separator)/2, len (@ Input ))
Set @ Index = charindex (@ Separator, @ Input)
End

Set @ Entry = ltrim (rtrim (@ Input ))
If (@ RemoveEmptyEntries = 0) or (@ RemoveEmptyEntries = 1 and @ Entry <> '')
Begin
Insert into @ TABLE ([Value]) Values (@ Entry)
End

Return
End

How to use:

The Code is as follows:

Declare @ str1 varchar (max), @ str2 varchar (max), @ str3 varchar (max)

Set @ str1 = '1, 2, 3'
Set @ str2 = '1 ### 2 ### 3'
Set @ str3 = '1 #### 2 ### 3 ###'

[Value] from [dbo]. [SplitString] (@ str1, ',', 1)
Select [Value] from [dbo]. [SplitString] (@ str2, '###', 1)
Select [Value] from [dbo]. [SplitString] (@ str3, '###', 0)


There is also an auto-increment [Id] field, which may be used in some cases, such as saving and sorting by Id.

For example, sort by table ID:

The Code is as follows:

Update a set. [Order] = t. [Id] from [dbo]. [Table] as a join [dbo]. splitString ('1, 2, 3 ', 1) as t on. [Id] = t. [Value]

Instance

The Code is as follows:

Create function [dbo]. [split]
(
@ SourceSql varchar (8000 ),
@ StrSeprate varchar (10)
)
Returns @ temp table (F1. varchar (100 ))
As
Begin
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 + 1, len (@ SourceSql)-@ I)
Set @ I = charindex (@ StrSeprate, @ SourceSql)
End
If @ SourceSql <>''
Insert @ temp values (@ SourceSql)
Return
End

Select * from split (', 777, 11, 888, 88, 1122,888, 88,77, 00, 00 ',',')

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.