separating a text field where info divided by a dash

  • Example

    Col1 = FIN - TIN - LIN - BIN

     

    Need to take col1 and make 4 columns out of it.

    col1 = FIN

    col2 = TIN

    col3 = LIN

    col4 = BIN

    Isn't this charindex or something like that?

     

  • Check this out:

    http://www.sqlservercentral.com/columnists/sjones/20010424135929_1.asp

    Charindex or patindex will work.

  • IF you will always have 4 columns, then this will work nicely...

    DECLARE @TestText VARCHAR(100)

        SET @TestText = 'FIN - TIN - LIN - BIN'

     SELECT PARSENAME(REPLACE(@TestText,' - ','.'),4) AS FirstCol,

            PARSENAME(REPLACE(@TestText,' - ','.'),3) AS SecondCol,

            PARSENAME(REPLACE(@TestText,' - ','.'),2) AS ThirdCol,

            PARSENAME(REPLACE(@TestText,' - ','.'),1) AS FourthCol

    IF you have a variable number of columns or more than 4 columns, we'll have to resort to other measures... let us know, please.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 3 posts - 1 through 2 (of 2 total)

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