• Jeff Moden (6/6/2013)


    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)

    How come neither of us got that simple and effective solution? I need to go back to practice the KISS mantra.

    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