• Luis Cazares (6/6/2013)


    Sean Lange (6/6/2013)


    Here is another:

    select stuff(isnull(col1 + '/', '') + isnull(col2 + '/', ''), LEN(isnull(col1 + '/', '') + isnull(col2 + '/', '')), 1, '')

    from Table_1

    Sean, you gave me an idea.

    select stuff(isnull('/' + col1, '') + isnull('/' + col2, ''), 1, 1, '')

    from Table_1

    No wonder my post count is so low lately. Between you, Sean, and some of the other heavy hitters, I don't have much to do anymore. Well done!

    If you're guaranteed to have only 2 columns, a slight variation on the COALESCE approach would work. Haven't checked it for performance, but it might be a little faster because it only does 2 concatenations instead of 3.

    SELECT COALESCE(Col1+'/'+Col2,Col1,Col2)

    --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)