Split character sting - Selected Part

  • I am looking for help on splitting a string and select nth occurrence only to display like

    @String='123,0.934,98,928.34,987.45'

    The above string contains 5 values delimited by Comma.

    I need to display only 3rd split - 98

    I need to display only 5th split value - 987.45

    I could find functions to display all values together, but I want only nth occurrence only.

    I appreciate your kind help.

  • You could use the delimited split function by Jeff Moden:

    Tally OH! An Improved SQL 8K “CSV Splitter” Function[/url]

    It returns a table with the various parts and their positions, so you select only those you need.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Thanks for the reply.

    I reviewed your suggestion, would you please let me know how can I use the function run time in a SQL insert statement.

  • Use it as any other TVF

    DECLARE @String varchar(100)='123,0.934,98,928.34,987.45'

    SELECT *

    FROM dbo.DelimitedSplit8K(@String, ',')

    WHERE ItemNumber IN(3,5)

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Ramesh Mutyapu (10/7/2013)


    Thanks for the reply.

    I reviewed your suggestion, would you please let me know how can I use the function run time in a SQL insert statement.

    Come now, you have to do at least a little bit of the effort yourself 😉

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply