Returns the nth character of a string with a specific delimiter.

Source: Internet
Author: User

/*

Returns the nth character of a string with a specific delimiter.

*/
-- Select DBO. getsplit ('a: B: C: D: e', ':', 2)
Create Function getsplit (@ nvsourcesql nvarchar (4000), @ strseprate varchar (10), @ IPOs INT)
Returns varchar (100)
-- Implement the nth character split function to get a string with a specific delimiter
-- Date: 2006-05-26
-- Author: xw_cai
As
Begin
Declare @ I int, @ vreturnvalue varchar (100)
, @ Istrcount int
Select @ nvsourcesql = rtrim (ltrim (@ nvsourcesql ))
, @ I = charindex (@ strseprate, @ nvsourcesql)
, @ Vreturnvalue =''
, @ Istrcount = Len (LEN (@ nvsourcesql)-len (replace (@ nvsourcesql, @ strseprate, '') + 1

If (@ istrcount <@ IPOS) -- returns a null value when the number of characters entered exceeds the range.
Begin
Return''
End

If (right (@ nvsourcesql, 1) <> @ strseprate)
Begin
Set @ nvsourcesql = @ nvsourcesql + @ strseprate
End
While @ IPOs> = 1
Begin
Set @ vreturnvalue = left (@ nvsourcesql, @ i-1)
Set @ nvsourcesql = substring (@ nvsourcesql, @ I + 1, Len (@ nvsourcesql)-@ I)
Set @ I = charindex (@ strseprate, @ nvsourcesql)
Set @ IPOs =@iPos-1
End
Return @ vreturnvalue
End

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.