?
Solved

Conditioning in the WHERE clause

Posted on 2006-07-02
5
Medium Priority
?
351 Views
Last Modified: 2008-03-04
Hello.

Let say I have a date field and it can be NULL. I query this table in my store procedure and depending on the parameter, I want to select either all values WITH NULL and WITHOUT NULL, only values WITH NULL,  or only values WITHOUT NULLS.

I tried to put CASE and IF into my WHERE clause but apparently it's either not supposed to be working there or I had a wrong syntax. Anyway, my preudo-script is:

SELECT * FROM my_table
WHERE
CASE @UseNulls
   WHEN 1 THEN ( my_date IS NULL )
   WHEN 0 THEN ( my_date IS NOT NULL )
   WHEN 2 THEN ?? /* to have both */ ??

END

Thanks,
Mik

Thanks
0
Comment
Question by:Yurich
5 Comments
 
LVL 12

Accepted Solution

by:
Einstine98 earned 1200 total points
ID: 17027908
SELECT * FROM my_table
WHERE
(My_Date IS NULL AND @UseNulls = 1)
OR (My_Date IS NOT NULL AND @UseNulls = 0)
OR @UseNulls = 2

Case does work, but not the way you used it...

 SELECT * FROM my_table
WHERE
CASE
          WHEN My_Date IS NULL THEN 'YES'
          ELSE 'NO'
     END = CASE @UseNulls
   WHEN 1 THEN 'Yes'
   WHEN 0 THEN 'No'
END
OR @UseNulls = 2
0
 
LVL 12

Expert Comment

by:Einstine98
ID: 17027962
Hey... just had another though (which I always do :-) )

SELECT * FROM my_table
WHERE
CASE
      WHEN My_Date IS NULL THEN '1'
        ELSE '0'
END = @UseNulls
OR @UseNulls = 2
0
 
LVL 17

Assisted Solution

by:HuyBD
HuyBD earned 400 total points
ID: 17029407
Try this.

SELECT * FROM my_table
WHERE
NULLIF(my_date,'NIL') =
CASE @UseNulls
   WHEN 1 THEN 'NIL'
   WHEN 0 THEN  my_date
   WHEN 2 THEN NULLIF(my_date,'NIL')
END

Hope this help
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 400 total points
ID: 17030717
SELECT      *
FROM      my_table
WHERE       @UseNulls = 1 And my_date IS NULL
      Or @UseNulls = 0 And my_date IS NOT NULL
      Or @UseNulls = 2

Or if you prefer to read it this way:

SELECT      *
FROM      my_table
WHERE       (@UseNulls = 1 And my_date IS NULL)
      Or (@UseNulls = 0 And my_date IS NOT NULL)
      Or (@UseNulls = 2)
0
 
LVL 21

Author Comment

by:Yurich
ID: 17034239
thank you guys,

case ... = case ... is especially interesting as I didn't know you can do it like that

regards,
yurich
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question