Solved

Conditioning in the WHERE clause

Posted on 2006-07-02
5
252 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

910 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

16 Experts available now in Live!

Get 1:1 Help Now