Saturday, February 25, 2012

Null to Zero in Select Statement?

How can I do a null to 0 in a select statement? I tried the NZ() function but it is not part of SQL.

Thanks very much,you can use CASE stmt ...chk BOL for for more info

hth|||Easier, ISNULL(). If the field that might be null is an int called MyField:

SELECT IsNull(MyField,0) FROM table|||or COALESCE is the other option. i'm not always sure when ISNULL or COALESCE is more appropriate

cs|||COALESCE allows as many arguments as you want, that is:

COALESCE(v1,v2,v3,v4,0)

Will return 0 if all of v1 through v4 are null, otherwise the first non-null argument...|||that's right, I remember now. COALESCE is good if you want to pass in multiple values and get a zero if all are null, but not useful if you want to substitute another value for the null. I had forgotten about the multiple argument thing.

cs

No comments:

Post a Comment