Logic with dates

I have 3 dates. The 3 dates are eff_dt, exp_dt and ins_dt and I want to create a function and the logic would be as follows
1. Check for valid dates and move on to step 2 after checks are complete
  a. eff_dt check for valid date and no null
  b. exp_dt check for valid date and no null
  c. ins_dt check for valid date and null is ok
2. Check ins_dt >= exp_dt  (if True then Function --> function returns Good  is done)
3. Check ins_dt = eff_dt  (if True then Function --> function returns BAD and is done)
4. Check Todays Date > ins_dt  (if True then Function --> function  returns BAD and is done)

If all meet criteria then return GOOD

I have a CASE statement but it seems sloppy to me. How would you go about designing this function?
jknj72Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jknj72Author Commented:
doing this in SQL Developer
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Eyeballeth thy article on T-SQL: Identify bad dates in a time series and let me know how close it comes to what you're trying to pull off here.   If I'm interpreting this correctly you can modify the big honkin' nested Common Table Expression (CTE) to meet your needs, and pull this off as a set instead of a single row / single value function.
0
johnsoneSenior Oracle DBACommented:
First thing to me is define a valid date.  If these are stored in fields that are of DATE datatype, then they are valid.

What is the end goal here?  I believe that you could create constraints to handle these and then there is no need for checking.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jknj72Author Commented:
Jim - I will check the article out ..Thanks

Johnsone -  Data comes from tables that allow Nulls and data types are Date but I cant edit tables.. I have to check to make sure there are dates in the first 2 fields and Nulls are not acceptable in my logic and the 3rd field check for a date and a Null will be acceptable to move to the next step..

Im doing this in Oracle(SQL Developer) sdstuber?
0
johnsoneSenior Oracle DBACommented:
If the data type is Date, then it must be a valid date.

The tool doesn't matter.  The database does.  All SQL Developer is doing is sending a query to SQL Server over a connection.  It really isn't involved.
0
jknj72Author Commented:
thanks sdstuber!!

johnsone, I guess that makes sense and doesn't need to check for a VALID date.  I do however need to check the rest of the logic. How would you do it? I just want a function to return (true or false) or (0 or 1) or whatever to let me know if it passes the checks. I guess im just gonna keep the CASE statement I have, or maybe have an If elsif statement,  I just thought maybe Id get some other opinions...
0
sdstuberCommented:
I would use CASE, I don't know what your original case looked like but given your rules, I would use something like this...

SELECT CASE
           WHEN eff_dt IS NULL THEN 'Bad'
           WHEN exp_dt IS NULL THEN 'Bad'
           WHEN ins_dt IS NULL THEN 'Good'
           WHEN ins_dt >= exp_dt THEN 'Good'
           WHEN ins_dt = eff_dt THEN 'Bad'
           WHEN TRUNC(SYSDATE) > ins_dt THEN 'Bad'
           ELSE 'Unknown'
       END
  FROM your_table

if you want to turn it into a function, then same thing...


CREATE OR REPLACE FUNCTION check_dates(eff_dt IN DATE, exp_dt IN DATE, ins_dt IN DATE)
    RETURN VARCHAR2
IS
BEGIN
    RETURN CASE
               WHEN eff_dt IS NULL THEN 'Bad'
               WHEN exp_dt IS NULL THEN 'Bad'
               WHEN ins_dt IS NULL THEN 'Good'
               WHEN ins_dt >= exp_dt THEN 'Good'
               WHEN ins_dt = eff_dt THEN 'Bad'
               WHEN TRUNC(SYSDATE) > ins_dt THEN 'Bad'
               ELSE 'Unknown'
           END;
END;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jknj72Author Commented:
pretty much what I have sdstuber and im testing now and so far all looks good. Thanks for the help
0
jknj72Author Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.