SQL Server functions implement the C # split Function

Source: Internet
Author: User

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:

Related Article

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.