Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Conditioning in the WHERE clause

Posted on 2006-07-02
5
Medium Priority
?
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

618 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