testing for null and not null in case statement

testing for null and not null in case statement

Post by d.s. » Thu, 07 Aug 2008 01:30:36


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

Post by Eric Isaac » Thu, 07 Aug 2008 01:36:34

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

Post by Plamen Rat » Thu, 07 Aug 2008 01:38:37

You have to use the searched CASE syntax and use IS NULL to check for NULL
values:

CASE WHEN activate_date IS NULL
THEN 'active next bill date'
ELSE 'active'
END


Plamen Ratchev
http://www.yqcomputer.com/
 
 
 

testing for null and not null in case statement

Post by Peter DeBe » Thu, 07 Aug 2008 01: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

Post by d.s. » Thu, 07 Aug 2008 01:53:13


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

Post by d.s. » Thu, 07 Aug 2008 01:56:00


> ELSE 'a>tive' >> >ND
>
> Plamen Ratchev http://www.yqcomputer.com/

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

Post by Plamen Rat » Thu, 07 Aug 2008 02: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.yqcomputer.com/
 
 
 

testing for null and not null in case statement

Post by d.s. » Thu, 07 Aug 2008 02:21:16


> > THEN 'active next bill dat>'> > > ELSE 'a>t>ve'
>>> >N>
>
> > Plamen Ratchev http://www.yqcomputer.com/ >o.>om
>
> CASE activat>_date
> WHEN IS NULL THEN 'active next bill>date'
> ELSE 'active' END AS>Ex>r2
>
> Error in list of function arguments: 'IS' not recognized. (THIS>ERROR
> IS DUE TO THE 'IS' STAT>MENT)
> Error in list of function arguments: ',' not recognized. THI> ERROR
> IS PROBABLY BECAUSE THE 'IS' ERROR MUNGED THE INTER>RETOR)
> Unable to parse query text.

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://www.yqcomputer.com/
 
 
 

testing for null and not null in case statement

Post by Eric Isaac » Thu, 07 Aug 2008 02:29:04

> Ok, I found it on a website:

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

Post by d.s. » Thu, 07 Aug 2008 02:37:49


Sorry, I missed that. That's what I get for speed reading. Thanks.
 
 
 

testing for null and not null in case statement

Post by --CELKO- » Thu, 07 Aug 2008 02:37:53

he 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 specification> ::= <simple case> | <searched case>

<simple case> ::=
CASE <case operand>
<simple when clause>...
[<else clause>]
END

<searched case> ::=
CASE
<searched when clause>...
[<else clause>]
END

<simple when clause> ::= WHEN <when operand> THEN <result>

<searched when clause> ::= WHEN <search condition> THEN <result>

<else clause> ::= ELSE <result>

<case operand> ::= <value expression>

<when operand> ::= <value expression>

<result> ::= <result expression> | NULL

<result expression> ::= <value expression>

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
 
 
 

testing for null and not null in case statement

Post by d.s. » Thu, 07 Aug 2008 02:45:32


Sorry, I just focused on the inclusion of the IS. I didn't clue into
the other change.