• wendy elizabeth (3/26/2015)


    In t-sql 2012 there is the coalesce statement being used twice in a where clause. I do not understand what the where clause is doing. Thus can you explain what the where clause means in the following statement where coalesce is being used twice:

    where coalesce(table1.status, table2.status) = 'A'

    and coalesce(table1.code, 'UNV') in ('ABS', 'EUF', 'UNV', 'LEG', ILL', 'SUP')

    Thus would you tell me what the above query means?

    It's easier to start by explaining how coalesce works: it returns the first non-null value it encounters. If all of its arguments are null, it returns null.

    Your first example (where coalesce(table1.status, table2.status) = 'A')

    Is table1.status = 'A'? Condition is true

    Is table1.status <> 'A' and not null? Condition is false.

    But ... If table1.status is null:

    check table2.status

    Is table2.status = 'A'? Condition is true

    otherwise: Condition is false

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.