In the string function of SQL Server, there is no function similar to the split function of C #. The following function implements this function:
Code
Use [ Database_name ]
Go
Set Quoted_identifier On
Go
-- ========================================================== =====
-- Author: arui
-- Create Date: 2008-03-19
-- Description: Split Function
-- Debug: Select * From DBO. fun_split ('abc: BC: C: D: e ',':')
-- ========================================================== =====
Alter Function [ DBO ] . [ Fun_split ]
(
@ Sourcesql Varchar ( 8000 ),
@ Strseprate Varchar ( 10 )
)
Returns
@ Temp_table Table ( Varchar ( 100 ))
As
Begin
Declare @ I Int
Set @ Sourcesql = Rtrim ( Ltrim ( @ Sourcesql ))
Set @ I = Charindex ( @ Strseprate , @ Sourcesql )
While @ I > = 1
Begin
Insert @ Temp_table Values ( Left ( @ Sourcesql , @ I - 1 ))
Set @ Sourcesql = Substring ( @ Sourcesql , @ I + 1 , Len ( @ Sourcesql ) - @ I )
Set @ I = Charindex ( @ Strseprate , @ Sourcesql )
End
If @ Sourcesql <> ' \ '
Insert @ Temp_table Values ( @ Sourcesql )
return
end
call: select * From DBO. fun_split ('abc: BC: C: D: e', ':')
result: