Solved

Filtering out data in a WHERE clause based on CASE statement

Posted on 2007-12-03
5
212 Views
Last Modified: 2010-04-21
Hello Gurus,
In my SELECT statement I have several CASE statements.  The 'ELSE' in one statement = NULL.  I do not want to select the data where NULL is returned.  For example, in the CASE statement, I am managing the company name from several different data sources with the ELSE = NULL.  There are some instances in a specific data source where there is a data record and the 'cleansed' company name returned is NULL.  I want to exclude that data from the SELECT.  I'm assuming I should be able to do that in the WHERE clause but have been unsuccessful.  Your assistance is greatly appreciated.

0
Comment
Question by:hypermac
  • 3
5 Comments
 
LVL 1

Expert Comment

by:wizengamot
ID: 20399932
When you filter out NULL values the syntax is as follows in the code block below.  I hope this is what your looking for, if not, then please elaborate a bit more and I will do my best to assist further.

Please note the lack of =Null as this syntax is not supported.  An object cannot be Equal to Null because Null is Nothing and an object that is equal to by definition must have a value (the concept that it is equal means that it is equal to something other than nothing)..... This concept is one of the hardest to wrap your head around when dealing with NULL in a database.  If you get that, your well on your way to being a guru yourself.

SELECT * FROM tbSampleTable WHERE NOT SampleField IS NULL

Open in new window

0
 

Author Comment

by:hypermac
ID: 20400013
Thanks for such a quick.  Here's the CASE statement.
(CASE WHEN aa.ROW_SOURCE_DESC = 'MFS' AND CONFORMED_COMPANY_NUMBER IS NOT NULL THEN'Co'||LPAD(bb.CONFORMED_COMPANY_NUMBER,3,'0')
        WHEN aa.ROW_SOURCE_DESC IN ('ARTESIA', 'ARTESIA_OTHER') THEN 'Co'||LPAD(bb.COMPANY_NUMBER,3,'0')
        ELSE NULL
        END) AS COMPANY,
I do not want the query to return any records where the COMPANY is NULL.  How do I add this criteria to my WHERE clause?  Is that the correct place to do it?  
0
 
LVL 11

Accepted Solution

by:
yuching earned 500 total points
ID: 20400248
You have 2 options
1.) Include the case when in where query
WHERE (CASE WHEN aa.ROW_SOURCE_DESC = 'MFS' AND CONFORMED_COMPANY_NUMBER IS NOT NULL THEN'Co'||LPAD(bb.CONFORMED_COMPANY_NUMBER,3,'0')
        WHEN aa.ROW_SOURCE_DESC IN ('ARTESIA', 'ARTESIA_OTHER') THEN 'Co'||LPAD(bb.COMPANY_NUMBER,3,'0')
        ELSE NULL
        END) IS NOT NULL

2.) Or if lazy to write because too many case when, then you can select from the subquery eg.

SELECT Company
FROM
(
       SELECT (CASE WHEN aa.ROW_SOURCE_DESC = 'MFS' AND CONFORMED_COMPANY_NUMBER IS  
            NOT NULL THEN'Co'||LPAD(bb.CONFORMED_COMPANY_NUMBER,3,'0')
            WHEN aa.ROW_SOURCE_DESC IN ('ARTESIA', 'ARTESIA_OTHER') THEN 'Co'||LPAD
           (bb.COMPANY_NUMBER,3,'0')
          ELSE NULL
           END)
         FROM yourtable
) WHERE Company IS NOT NULL
0
 

Author Comment

by:hypermac
ID: 20403532
Thanks yuching!  For some reason I thought I couldn't use a CASE statement in a WHERE clause.  DUH!
0
 

Author Closing Comment

by:hypermac
ID: 31412482
Got me where I needed to go!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to take different types of Oracle backups using RMAN.

757 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

17 Experts available now in Live!

Get 1:1 Help Now