?
Solved

Conditioning in the WHERE clause

Posted on 2006-07-02
5
Medium Priority
?
314 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

777 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