I have inherited a table that has NULL values for float
fields, instead of a 0 value. I am doing some basic testing for querying, and in my tests both ISNULL()
and COALESCE()
present the same accurate output. In my testing (a table with roughly 5K records) time difference is very negligible as well.
Question being with the sample DDL below, is there a benefit to use ISNULL()
over COALESCE()
or vice-versa? Are there circumstances that I have yet to see that could provide an inaccurate calculation?
Create Table #Test
(
blue float
,red float
,green float
)
Insert Into #Test VALUES
('14', NULL, '12')
,(NULL, '12', '10')
,(NULL, NULL, '8')
,('10', '2', NULL)
Select
ISNULL(blue,0)+ISNULL(red,0)+ISNULL(green,0)
FROM #Test
SELECT
COALESCE(blue,0)+COALESCE(red,0)+COALESCE(green,0)
FROM #Test
Best Answer
COALESCE can take a whole list of values to check for NULL where ISNULL takes one check value and would require nesting to check more values.
vs
Since your case is using float values, there should not be a difference in results from calculations. The only benefit I can really see if code readability and minimizing the amount of nesting needed.