SQL query and if statement

Posted on 2014-03-03
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.
Question by:Lee R Liddick Jr
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 22

Assisted Solution

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

Assisted Solution

by:David Kroll
David Kroll earned 150 total points
ID: 39900424
  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)

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.
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf


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)
     a.c_eff_dt is NULL
          AND b.c_eff_dt < date(current date - 6 months)

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.
LVL 52

Accepted Solution

_agx_ earned 200 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)  )
     ( a.c_eff_dt is NULL AND b.c_eff_dt < date(current date - 6 months)  )

Author Comment

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

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.

Featured Post

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

632 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