SQL Server Performance

How to convert NULL to 0 ?

Discussion in 'General DBA Questions' started by xiebo2010cx, Apr 2, 2006.

  1. xiebo2010cx Member

    I met a question, my customer wants me to output a table, if the value of a column is NULL, then output 0, what function should I use to do this?
  2. Adriaan New Member

    SELECT ISNULL(column, 0) FROM MyTable
  3. xiebo2010cx Member

    Thank you,Adriaan. I just tested it, it works!

    Any other ways to do it, I think it must be at least 3 way to do it!
  4. Adriaan New Member

    ISNULL is more to the point than the two alternatives that I know:

    SELECT CASE WHEN MyColumn IS NULL THEN 0 ELSE MyColumn END FROM MyTable

    SELECT COALESCE(MyCoumn, 0) FROM MyTable


    If you're coming from a regular programming language, perhaps you think that this ...

    SELECT MyColumn + 0

    ... might work, but NULL plus anything is still NULL in T-SQL.
  5. xiebo2010cx Member

    appreciate it! Adriaan!

    I got a lot!
  6. Madhivanan Moderator

    >>I think it must be at least 3 way to do it!

    How did you think without knowing any syntaxes?

    Madhivanan

    Failing to plan is Planning to fail

Share This Page