Use SQL split

Source: Internet
Author: User
Code
Set Ansi_nulls On
Set Quoted_identifier On
Go

Alter   Function   [ DBO ] . [ F_split ] ( @ Temp   Varchar ( 8000 ), @ Parting   Char ( 1 ))
-- F_split
-- @ Temp is the parameter name. varchar is the parameter type. 8000 is the type length. @ parting is the parameter name. Char type.
-- Split SQL string
Returns   Varchar ( 8000 )
-- The return type is varchar. The length is 4000.
As  
Begin  
Declare   @ I   Int , @ Str   Char ( 15 ), @ Formattemp   Char ( 12 ), @ S   Varchar ( 8000 )
-- Set @ temp = '2017 0022022,0000022017, 100'
Set   @ S = ''

Set   @ Temp = Rtrim ( Ltrim ( @ Temp ))
Set   @ I = Charindex ( @ Parting , @ Temp ) -- Search for the start position of "." From the start position of temp
-- Create Table # T (IP char (15) -- create a temporary table
While   @ I > = 1

Begin
Set   @ Str = Left ( @ Temp , @ I - 1 )
-- Insert into # T values (@ Str)

Set   @ S = @ S + '''' + Rtrim ( Ltrim ( @ Str )) + '''' + ' , '
Set   @ Temp = Substring ( @ Temp , @ I + 1 , Len ( @ Temp ) - @ I )
Set   @ I = Charindex ( @ Parting , @ Temp )
End
-- Set @ s = @ s + ''' + rtrim (ltrim (@ Str) + ''' + ','
Set   @ S = @ S + '''' + @ Temp + ''''
Return   Substring ( @ S , 1 , Len ( @ S ))
End  

Print DBO. f_split ( ' 000002hz0000022022, ' , ' , ' )

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.