Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

testing for null and not null in case statement

 
   Database Help (Home) -> Programming RSS
Next:  Trigger code not working as expected  
Author Message
d.s.

External


Since: Jan 29, 2008
Posts: 29



(Msg. 1) Posted: Tue Aug 05, 2008 9:30 am
Post subject: testing for null and not null in case statement
Archived from groups: microsoft>public>sqlserver>programming (more info?)

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.

 >> Stay informed about: testing for null and not null in case statement 
Back to top
Login to vote
Eric Isaacs

External


Since: May 13, 2008
Posts: 367



(Msg. 2) Posted: Tue Aug 05, 2008 9:36 am
Post subject: Re: testing for null and not null in case statement [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

This should work...

case
when activate_date IS NULL then 'active next bill date'
else 'active'
end

-Eric Isaacs

 >> Stay informed about: testing for null and not null in case statement 
Back to top
Login to vote
d.s.

External


Since: Jan 29, 2008
Posts: 29



(Msg. 3) Posted: Tue Aug 05, 2008 9:53 am
Post subject: Re: testing for null and not null in case statement [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Aug 5, 9:36 am, Eric Isaacs wrote:
> This should work...
>
> case
> when activate_date IS NULL then 'active next bill date'
> else 'active'
> end
>
> -Eric Isaacs

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.
 >> Stay informed about: testing for null and not null in case statement 
Back to top
Login to vote
d.s.

External


Since: Jan 29, 2008
Posts: 29



(Msg. 4) Posted: Tue Aug 05, 2008 9:56 am
Post subject: Re: testing for null and not null in case statement [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Aug 5, 9:38 am, "Plamen Ratchev" wrote:
> 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 Ratchevhttp://www.SQLStudio.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.
 >> Stay informed about: testing for null and not null in case statement 
Back to top
Login to vote
d.s.

External


Since: Jan 29, 2008
Posts: 29



(Msg. 5) Posted: Tue Aug 05, 2008 10:21 am
Post subject: Re: testing for null and not null in case statement [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Aug 5, 9:56 am, "d.s." wrote:
> On Aug 5, 9:38 am, "Plamen Ratchev" wrote:
>
> > 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 Ratchevhttp://www.SQLStudio.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.

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=91&...eadid=8
 >> Stay informed about: testing for null and not null in case statement 
Back to top
Login to vote
Eric Isaacs

External


Since: May 13, 2008
Posts: 367



(Msg. 6) Posted: Tue Aug 05, 2008 10:29 am
Post subject: Re: testing for null and not null in case statement [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> Ok, I found it on a website:
>
> CASE WHEN activate_date IS NULL
> THEN 'active next bill date'
> ELSE 'active' END AS Expr1

We posted this exact statement above without the redundant AS Expr1,
but you said it didn't work for you.

-Eric Isaacs
 >> Stay informed about: testing for null and not null in case statement 
Back to top
Login to vote
d.s.

External


Since: Jan 29, 2008
Posts: 29



(Msg. 7) Posted: Tue Aug 05, 2008 10:37 am
Post subject: Re: testing for null and not null in case statement [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Aug 5, 10:08 am, "Plamen Ratchev" wrote:
> 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 Ratchevhttp://www.SQLStudio.com

Sorry, I missed that. That's what I get for speed reading. Thanks.
 >> Stay informed about: testing for null and not null in case statement 
Back to top
Login to vote
--CELKO--

External


Since: Jan 11, 2008
Posts: 1089



(Msg. 8) Posted: Tue Aug 05, 2008 10:37 am
Post subject: Re: testing for null and not null in case statement [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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 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
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)
 >> Stay informed about: testing for null and not null in case statement 
Back to top
Login to vote
d.s.

External


Since: Jan 29, 2008
Posts: 29



(Msg. 9) Posted: Tue Aug 05, 2008 10:45 am
Post subject: Re: testing for null and not null in case statement [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Aug 5, 10:29 am, Eric Isaacs wrote:
> > Ok, I found it on a website:
>
> > CASE WHEN activate_date IS NULL
> > THEN 'active next bill date'
> > ELSE 'active' END AS Expr1
>
> We posted this exact statement above without the redundant AS Expr1,
> but you said it didn't work for you.
>
> -Eric Isaacs

Sorry, I just focused on the inclusion of the IS. I didn't clue into
the other change.
 >> Stay informed about: testing for null and not null in case statement 
Back to top
Login to vote
Peter DeBetta

External


Since: Mar 27, 2008
Posts: 2



(Msg. 10) Posted: Tue Aug 05, 2008 11:38 am
Post subject: Re: testing for null and not null in case statement [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

case
when activate_date is null then 'active next bill date'
else 'active'
end

"d.s." wrote in message

> 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.
 >> Stay informed about: testing for null and not null in case statement 
Back to top
Login to vote
Plamen Ratchev

External


Since: Jan 10, 2008
Posts: 1007



(Msg. 11) Posted: Tue Aug 05, 2008 12:38 pm
Post subject: Re: testing for null and not null in case statement [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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.SQLStudio.com
 >> Stay informed about: testing for null and not null in case statement 
Back to top
Login to vote
Plamen Ratchev

External


Since: Jan 10, 2008
Posts: 1007



(Msg. 12) Posted: Tue Aug 05, 2008 1:08 pm
Post subject: Re: testing for null and not null in case statement [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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
 >> Stay informed about: testing for null and not null in case statement 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
CASE WHEN NULL - A SQL Server 2005 DB table named 'Address' has the following records under the columns 'OrderID', 'UserID' & 'Addr': --------------------------------- OrderID UserID Addr --------------------------------- 1 12 35 Park Road 2 ...

Case When NULL - How do I do something like this? Case Field1 When NULL then 'AddressUpdate' Else Field1 End As MessageType When i do the preceeding I get Null instead of 'AddressUpdate' Thanks JC

Help with CASE and null value - Hi, I have difficulty in using the CASE in SQL view. I want to assign a status Open if EA_AllowanceID (INT) is null, else tag the field as 'Close'. The result set I got is always Close ==> CASE dbo.tblEducationAllowance.EA_AllowanceID WHEN '' THEN...

CASE for null or IsNULL - I want display the empty string if the field is NULL: 1)SELECT CASE when EmployeePhone is NULL then '' else EmployeePhone END FROM Employee 2) SELECT ISNULL(EmployeePhone, '') FROM Employee Is there any performance difference? I know may not..

view seems to ignore the 'concat null yields null' option - Hello all I have a database and am concatenatiing strings in a view. the value is coming up as null so I assumed that this was becasue 'concat null yields null' was set to true. I changed this setting (using the below) and checked it with the query o...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]