I have the following CASE
expression and the ISNULL
portion is not registering:
CASE WHEN IsNull(2010) THEN 0 END) *
CASE WHEN IsNumeric([Dur_In_Hours]) = 1 THEN
CAST([Dur_In_Hours] AS FLOAT) ELSE 0 END) AS Cost
I have the following CASE
expression and the ISNULL
portion is not registering:
CASE WHEN IsNull(2010) THEN 0 END) *
CASE WHEN IsNumeric([Dur_In_Hours]) = 1 THEN
CAST([Dur_In_Hours] AS FLOAT) ELSE 0 END) AS Cost
Unfortunately, you are using the IsNull()
function wrong. If you are testing if a column is null then you will use:
CASE
WHEN 2010 is null
THEN 0
END
*
CASE
WHEN IsNumeric([Dur_In_Hours]) =1
THEN CAST([Dur_In_Hours] AS FLOAT)
ELSE 0
END AS Cost
If you want to use IsNull()
then you can use IsNull([2010], 0)
to replace that CASE
expression:
IsNull([2010], 0)
*
CASE
WHEN IsNumeric([Dur_In_Hours]) =1
THEN CAST([Dur_In_Hours] AS FLOAT)
ELSE 0
END AS Cost
Or you can use COALESCE
to replace the first CASE
:
COALESCE([2010], 0)
*
CASE
WHEN IsNumeric([Dur_In_Hours]) =1
THEN CAST([Dur_In_Hours] AS FLOAT)
ELSE 0
END AS Cost
CASE
is an expression, not a statement. It's a subtle nit-pick but the distinction is important, IMHO.
I think you meant:
CASE WHEN [2010] IS NULL THEN 0 END
But that doesn't make any sense at all unless you are hard-coding 2010 there but plan to use a variable or column name later.
ISNULL
is something completely different than what you are after (at least as far as I can tell). It does not work the same way as it does in Access. For that reason I usually recommend COALESCE
instead, which functions similarly in most cases. I wrote this tip last year to help differentiate: