December 25, 2003 at 11:06 pm
one of my columns is retrieving null in a select statement
How do I convert this null value to 0 so that when the select statement executes it ouputs zero
and not null
Thanks
December 25, 2003 at 11:44 pm
SELECT COALESCE(NullColumn, 0) FROM ...
This will output the value in NullColumn if it is not null, otherwise 0.
--
Chris Hedgate @ Extralives (http://www.extralives.com/)
Contributor to Best of SQL Server Central 2002 (http://www.sqlservercentral.com/bestof/)
Articles: http://www.sqlservercentral.com/columnists/chedgate/
December 29, 2003 at 8:14 am
Though ISNULL does do the single alternative, COALESCE has the double added benefit of
1) being ANSI standard and supported bu more platforms and
2) allows multiple options.
If I'm testing several columns and want to return the first non-null one, I have to use COALESCE if the options number greater than two. As an example ...
select COALESCE(NullCol1, NullCol2, NullCol3, DefaultValue) from table
The first column with a non-null value is returned. If all are NULL, then the default is returned.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply