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, ' , ' , ' )