Adding SELECT values with a NULL

  • I just found out today that adding a NULL to any value results in a NULL.

    I have a SELECT statement that looks like this:

    SELECT WCAmount + WCHours AS Total FROM worktime WHERE WCid = 'WC11111'

    Now if either WCAmount or WCHours is NULL, then I get NULL back.

    Is there a way to still add the two columns together, and if one happens to be NULL, still get the value of the column that isn't NULL?

    Kind of like if NULL was 0, it would be 4+0...I will still get a 4 back even if the other value was 0. I still need to return one column's results even if the other is NULL. Now if they are both NULL, then getting a NULL back is fine.

    Any ideas?

    Thanks!

  • Basic stuff...

    SELECT COALESCE( WCAmount, 0 ) + COALESCE( WCHours, 0 ) AS Total FROM worktime

    WHERE WCid = 'WC11111'

     

    OR

     

    SELECT ISNULL( WCAmount, ) + ISNULL( WCHours,) AS Total FROM worktime

    WHERE WCid = 'WC11111'

    --Ramesh


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

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