Split-like string functions in the T-SQL

Source: Internet
Author: User
Tags rtrim

T-SQL on the string processing capabilities are relatively weak, such as I want to loop traversal like 1, 2, 3, 4, 5 such strings, if the use of arrays, traversal is very simple, but the T-SQL does not support the array, so it is troublesome to handle it. The following function implements string processing like an array.
I. Use a temporary table as an array
Create Function f_split (@ C varchar (2000), @ split varchar (2 ))
Returns @ t table (COL varchar (20 ))
As
Begin

While (charindex (@ split, @ C) <> 0)
Begin
Insert @ T (COL) values (substring (@ C, 1, charindex (@ split, @ C)-1 ))
Set @ C = stuff (@ C, 1, charindex (@ split, @ C ),'')
End
Insert @ T (COL) values (@ C)
Return
End
Go

Select * From DBO. f_split ('dfkd, dfdkdf, dfdkf, dffjk ',',')

Drop function f_split
Col
--------------------
Dfkd
Dfdkdf
Dfdkf
Dffjk

(The number of affected rows is 4)

2. Split the string by the specified symbol and return the number of elements after the split. The method is very simple, that is, to check the number of separators in the string, and then add one, which is the required result.
Create Function get_strarraylength
(
@ STR varchar (1024), -- string to be split
@ Split varchar (10) -- delimiter number
)
Returns int
As
Begin
Declare @ location int
Declare @ start int
Declare @ length int

Set @ STR = ltrim (rtrim (@ Str ))
Set @ location = charindex (@ split, @ Str)
Set @ length = 1
While @ location <> 0
Begin
Set @ start = @ location + 1
Set @ location = charindex (@ split, @ STR, @ start)
Set @ length = @ Length + 1
End
Return @ Length
End
Call example: Select DBO. get_strarraylength ('78, 1, 2, 3 ',',')
Return Value: 4

3. Split a string by a specified symbol. The number of elements of the specified index after the string is split is returned, as convenient as an array
Create Function get_strarraystrofindex
(
@ STR varchar (1024), -- string to be split
@ split varchar (10 ), -- separator
@ index int -- elements
)
Returns varchar (1024)
as
begin
declare @ location int
declare @ start int
declare @ next int
declare @ seed int

Set @ STR = ltrim (rtrim (@ Str ))
Set @ start = 1
Set @ next = 1
Set @ seed = Len (@ split)
 
Set @ location = charindex (@ split, @ Str)
While @ location <> 0 and @ index> @ next
Begin
Set @ start = @ location + @ Seed
Set @ location = charindex (@ split, @ STR, @ start)
Set @ next = @ next + 1
End
If @ location = 0 select @ location = Len (@ Str) + 1
-- There are two cases: 1. There is no Separator in the string. 2. There is a separator in the string. After jumping out of the while loop, @ location is 0, by default, there is a separator behind the string.
 
Return substring (@ STR, @ start, @ location-@ start)
End
Call example: Select DBO. get_strarraystrofindex ('8, 9, 4', ',', 2)
Return Value: 9

3. Combine the above two functions to traverse the elements in the string like an array

Declare @ STR varchar (50)
Set @ STR = '1, 2, 3, 4, 5'
Declare @ next int
Set @ next = 1
While @ next <= DBO. get_strarraylength (@ STR ,',')
Begin
Print DBO. get_strarraystrofindex (@ STR, ',', @ next)
Set @ next = @ next + 1
End

Call result:
1
2
3
4
5

TheArticleReproduced from the network base camp: http://www.xrss.cn/Info/7558.Html

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.