Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL query and if statement

Posted on 2014-03-03
9
Medium Priority
?
432 Views
Last Modified: 2014-03-09
In my where statement I need to check two areas, if the first isn't populated, check the other.

if a.C_EFF_DT is NOT NULL then a.C_EFF_DT < date(current date - 6 months)
if a.C_EFF_DT is NULL then b.C_EFF_DT < date(current date - 6 months)

So basically if the first C_EFF_DT is null then check the second C_EFF_DT, if it has a date, then check it and don't worry about the second date.
0
Comment
Question by:Lee R Liddick Jr
9 Comments
 
LVL 22

Assisted Solution

by:plusone3055
plusone3055 earned 600 total points
ID: 39900421
<cfif a.C_EFF_DT is NOT NULL>
 a.C_EFF_DT < date(current date - 6 months)
<cfelse>
 a.C_EFF_DT is NULL then b.C_EFF_DT < date(current date - 6 months)
</cfif>
0
 
LVL 11

Assisted Solution

by:David Kroll
David Kroll earned 600 total points
ID: 39900424
case
  when a.C_EFF_DT is NOT NULL then a.C_EFF_DT < date(current date - 6 months)
  when a.C_EFF_DT is NULL then b.C_EFF_DT < date(current date - 6 months)
  else (a date if the above two are false)
end
0
 

Author Comment

by:Lee R Liddick Jr
ID: 39900465
I didn't want this in CF...I am doing this in SAS SQL.  The case end in the where statement didn't work.  It doesn't like the less than sign...the query gives an error in the log.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Assisted Solution

by:Lee R Liddick Jr
Lee R Liddick Jr earned 0 total points
ID: 39901107
I figured it out myself, not even using 'if' or 'case' statements:
(
     a.c_eff_dt is NOT NULL
          AND a.c_eff_dt < date(current date - 6 months)
OR
     a.c_eff_dt is NULL
          AND b.c_eff_dt < date(current date - 6 months)
)
0
 

Author Comment

by:Lee R Liddick Jr
ID: 39902941
I've requested that this question be closed as follows:

Accepted answer: 0 points for leerljr68's comment #a39901107

for the following reason:

Accepting own comment since I figured it out myself with no assistance from the other expert comments.  Thank you.
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 800 total points
ID: 39901518
FYI, always wrap statements that mix AND/OR in parenthesis. Otherwise you're leaving the order of evaluation up to the db, which may or may not be what you want:

(
     <!--- first condition --->
     ( a.c_eff_dt is NOT NULL AND a.c_eff_dt < date(current date - 6 months)  )
OR
     ( a.c_eff_dt is NULL AND b.c_eff_dt < date(current date - 6 months)  )
)
0
 

Author Comment

by:Lee R Liddick Jr
ID: 39902942
I'm awarding points to those who responded.
0
 

Author Closing Comment

by:Lee R Liddick Jr
ID: 39915665
Even though I answered my own question, I'm awarding points for taking the time to respond.  Thank you all.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

972 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