using CASE IN A SCRIPT! URGENT

  • Hi All,

    first i want to perform a check on column whether if its null or not null. Then i want to check the next column for not null and null condition and then concatenate those values. How I should go about doing this. Right now I'm using CASE but I cannot seem to concatenate the output.

    Something like this:

    SELECT 'case_one' =

    CASE WHEN col_one IS NOT NULL THEN col_one ELSE 'column one is null' +','

    when col_two is not null then col_two

    else 'column two is null'

    end

    FROM TABLE_NAME

    The output I want is: col_one_value,col_one,col_two_value,col_two

    Thanks!

  • The simplest way is to break this into two separate CASE statements and concatenate the strings:

    SELECT

    CASE

    WHEN col_one IS NOT NULL THEN col_one

    ELSE 'column one is null'

    END

    + ',' +

    CASE

    WHEN col_two IS NOT NULL THEN col_two

    ELSE 'column two is NULL'

    END

    FROM TABLE_NAME

    K. Brian Kelley

    bkelley@sqlservercentral.com

    http://www.sqlservercentral.com/columnists/bkelley/

    K. Brian Kelley
    @kbriankelley

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

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