Solved

Conditioning in the WHERE clause

Posted on 2006-07-02
5
243 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 300 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 100 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 100 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help with SQL Query 23 39
C# Application Local DB Connection String 23 59
Convert int to military time 8 20
sql query Help 12 29
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now