[Original] [SQL function] SQL Server split Function

Source: Internet
Author: User

-- DBO. fnsplittovarchar

If exists (select * From information_schema.routines where routine_name = 'fnsplittovarchar ')
Begin
Drop function fnsplittovarchar
If not exists (select * From information_schema.routines where routine_name = 'fnsplittovarchar ')
Print 'drop function fnsplittovarchar succeeded'
Else
Print 'drop function fnsplittovarchar failed'
End
Else
Print 'attempting to create function dbo. fnsplittovarchar'
Go

Create Function DBO. fnsplittovarchar
(
@ Sourcesql varchar (8000 ),
@ Strseprate varchar (10)
)
Returns @ temp table (splitvalue varchar (500 ))

As
Begin
Declare @ I int
Declare @ strsepratelength int
Set @ strsepratelength = Len (@ strseprate)
Set @ sourcesql = rtrim (ltrim (@ sourcesql ))
Set @ I = charindex (@ strseprate, @ sourcesql)
While @ I> = 1
Begin
If @ I = 1
Insert @ temp values ('')
Else
Insert @ temp values (left (@ sourcesql, @ i-1 ))
Set @ sourcesql = substring (@ sourcesql, @ I + @ strsepratelength, Len (@ sourcesql)-@ I-@ strsepratelength + 1)
Set @ I = charindex (@ strseprate, @ sourcesql)
End
If @ sourcesql <> @ strseprate
Insert @ temp values (@ sourcesql)
Return
End

Go

If exists (select * From information_schema.routines where routine_name = 'fnsplittovarchar ')
Print 'create function DBO. fnsplittovarchar succeeded'
Else
Print 'create function DBO. fnsplittovarchar failed'
Go

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.