testing for null and not null in case statement - Peter DeBetta |
05-Aug-08 12:38:55
|
case
when activate_date is null then 'active next bill date'
else 'active'
end |
 |
| |
testing for null and not null in case statement - Plamen Ratchev |
05-Aug-08 01:08:07
|
As I noted, you have to use the searched CASE syntax. Just try the code as I
posted. You cannot use the simple CASE to check with IS NULL (you could use
COALESCE(activate_date, CAST('19000101' AS DATETIME)) and then check for
that base date, but really better to use the searched CASE).
Plamen Ratchev
http://www.SQLStudio.com |
 |
| |
testing for null and not null in case statement - d.s. |
08-Aug-08 09:35:12
|
I want to know whether a field has a date or not, and then group by
that result in a query.
I'm trying:
case activate_date
when null then 'active next bill date'
when not null then 'active'
end
and it didn't like that. Didn't like that "not" word in the third
line.
So then I went with:
case activate_date
when null then 'active next bill date'
else 'active'
end
but it's not picking up the one record I know is null. I'm just
getting all 'active'.
How do I phrase this to get the result I want?
Even a yes/no result would be fine. |
 |
| |
testing for null and not null in case statement - Eric Isaacs |
08-Aug-08 09:35:13
|
This should work...
case
when activate_date IS NULL then 'active next bill date'
else 'active'
end
-Eric Isaacs |
 |
| |
testing for null and not null in case statement - d.s. |
08-Aug-08 09:35:17
|
I tried that and it didn't like the syntax. Maybe I did something
wrong. I'll try it again. When I removed the IS, it accepted the
code. |
 |
| |
testing for null and not null in case statement - d.s. |
08-Aug-08 09:35:17
|
L
CASE activate_date
WHEN IS NULL THEN 'active next bill date'
ELSE 'active' END AS Expr2
Error in list of function arguments: 'IS' not recognized. (THIS ERROR
IS DUE TO THE 'IS' STATEMENT)
Error in list of function arguments: ',' not recognized. (THIS ERROR
IS PROBABLY BECAUSE THE 'IS' ERROR MUNGED THE INTERPRETOR)
Unable to parse query text. |
 |
| |
testing for null and not null in case statement - d.s. |
08-Aug-08 09:35:24
|
ULL
Ok, I found it on a website:
CASE WHEN activate_date IS NULL
THEN 'active next bill date'
ELSE 'active' END AS Expr1
The other syntax structure is when you have specific values to check
against, not nulls, or so says the website.
http://sqlforums.windowsitpro.com/web/forum/messageview.aspx?catid=3D91&thr=
eadid=3D87790&enterthread=3Dy |
 |
| |
testing for null and not null in case statement - Eric Isaacs |
08-Aug-08 09:35:25
|
We posted this exact statement above without the redundant AS Expr1,
but you said it didn't work for you.
-Eric Isaacs |
 |
| |
testing for null and not null in case statement - d.s. |
08-Aug-08 09:35:26
|
s I
se
Sorry, I missed that. That's what I get for speed reading. Thanks. |
 |
| |
testing for null and not null in case statement - --CELKO-- |
08-Aug-08 09:35:27
|
The CASE expression is an *expression* and not a control statement;
that is, it returns a value of one data type. SQL-92 stole the idea
and the syntax from the ADA programming language. Here is the BNF for
a <case specification>:
CASE <case operand>
[<else clause>]
END
CASE
[<else clause>]
END
The searched CASE expression is probably the most used version of the
expression. The WHEN ... THEN ... clauses are executed in left to
right order. The first WHEN clause that tests TRUE returns the value
given in its THEN clause. And, yes, you can nest CASE expressions
inside each other. If no explicit ELSE clause is given for the CASE
expression, then the database will insert a default ELSE NULL clause.
If you want to return a NULL in a THEN clause, then you must use a
CAST (NULL AS <data type>) expression. I recommend always giving the
ELSE clause, so that you can change it later when you find something
explicit to return.
The <simple case expression> is defined as a searched CASE expression
in which all the WHEN clauses are made into equality comparisons
against the <case operand>. For example
CASE iso_sex_code
WHEN 0 THEN 'Unknown'
WHEN 1 THEN 'Male'
WHEN 2 THEN 'Female'
WHEN 9 THEN 'N/A'
ELSE NULL END
could also be written as:
CASE
WHEN iso_sex_code = 0 THEN 'Unknown'
WHEN iso_sex_code = 1 THEN 'Male'
WHEN iso_sex_code = 2 THEN 'Female'
WHEN iso_sex_code = 9 THEN 'N/A'
ELSE NULL END
There is a gimmick in this definition, however. The expression
CASE foo
WHEN 1 THEN 'bar'
WHEN NULL THEN 'no bar'
END
becomes
CASE WHEN foo = 1 THEN 'bar'
WHEN foo = NULL THEN 'no_bar' -- error!
ELSE NULL END
The second WHEN clause is always UNKNOWN.
The SQL-92 Standard defines other functions in terms of the CASE
expression, which makes the language a bit more compact and easier to
implement. For example, the COALESCE () function can be defined for
one or two expressions by
1) COALESCE (<value exp #1>) is equivalent to (<value exp #1>)
2) COALESCE (<value exp #1>, <value exp #2>) is equivalent to
CASE WHEN <value exp #1> IS NOT NULL
THEN <value exp #1>
ELSE <value exp #2> END
then we can recursively define it for (n) expressions, where (n >= 3),
in the list by
COALESCE (<value exp #1>, <value exp #2>, . . ., n), as equivalent to:
CASE WHEN <value exp #1> IS NOT NULL
THEN <value exp #1>
ELSE COALESCE (<value exp #2>, . . ., n)
END
Likewise, NULLIF (<value exp #1>, <value exp #2>) is equivalent to:
CASE WHEN <value exp #1> = <value exp #2>
THEN NULL
ELSE <value exp #1> END
It is important to be sure that you have a THEN or ELSE clause with a
data type that the compiler can find to determine the highest data
type for the expression.
A trick in the WHERE clause is use it for a complex predicate with
material implications.
WHERE CASE
WHEN <search condition #1>
THEN 1
WHEN <search condition #2>
THEN 1
...
ELSE 0 END = 1
Gert-Jan Strik posted some examples of how ISNULL() and COALESCE()
work
CREATE TABLE #t(a CHAR(1));
INSERT INTO #t VALUES (NULL);
SELECT ISNULL(a,'abc') FROM #t;
SELECT COALESCE(a, 'abc') FROM #t;
DROP TABLE #t;
He always use COALESCE, with the exception of the following type of
situation, because of its performance consequences:
SELECT ...,
ISNULL((SELECT COUNT(*) -- or other aggregate
FROM B
WHERE B.key = A.key), 0)
FROM A;
Likewise, Alejandro Mesa cam up with this example:
SELECT 13 / COALESCE(CAST(NULL AS INTEGER), 2.00); -- promote to
highest type (decimal)
SELECT 13 / ISNULL(CAST(NULL AS INTEGER), 2.00); -- promote to first
type (integer) |
 |
| |
testing for null and not null in case statement - d.s. |
08-Aug-08 09:35:28
|
Sorry, I just focused on the inclusion of the IS. I didn't clue into
the other change. |
 |
| |