Split function in SQL

Source: Internet
Author: User
Source Address: http://blog.csdn.net/smile2me27/archive/2004/07/08/37107.aspx
1. Parameter definition and description
Output parameter: @ splitString varchar (8000) -- string to be Split
@ Separate varchar (10) --- Separator
Return: @ returnTable table (col_Value varchar (20) -- the length of a single character can be modified as needed

2. Function body
Create function SplitStr (@ splitString varchar (8000), @ separate varchar (10 ))
RETURNS @ returnTable table (col_Value varchar (20 ))
AS
BEGIN
Declare @ thisSplitStr varchar (20)
Declare @ thisSepIndex int
Declare @ lastSepIndex int
Set @ lastSepIndex = 0
If Right (@ splitString, len (@ separate) <> @ separate set @ splitString = @ splitString @ separate
Set @ thisSepIndex = CharIndex (@ separate, @ splitString, @ lastSepIndex)
While @ lastSepIndex <= @ thisSepIndex
Begin
Set @ thisSplitStr = SubString (@ splitString, @ lastSepIndex, @ thisSepIndex-@ lastSepIndex)
Set @ lastSepIndex = @ thisSepIndex 1
Set @ thisSepIndex = CharIndex (@ separate, @ splitString, @ lastSepIndex)
Insert into @ returnTable values (@ thisSplitStr)
End
Return
END

The following is an example:
Example: select * from SplitStr ('20170101 ',',')
Result:
Col_value
-----------------------------------------------------------------------------
123
456
789

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.