Solved

Filtering out data in a WHERE clause based on CASE statement

Posted on 2007-12-03
5
215 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

772 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