×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

SQL code to check for records 365 prior to today

SQL code to check for records 365 prior to today

SQL code to check for records 365 prior to today

(OP)
I would like to write some SQL to gather all of the records that meet the condition of todays date and everything prior to 365 days...

where field1 (DATE now <= 365 days)

can someone provide a better way of doing this in SQL?  Database is Oracle 11g.

Thanks!

 

Dano
What's your major malfunction

RE: SQL code to check for records 365 prior to today

suggest you post this question in one of the Oracle forums

you might not like the ANSI SQL solution

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon

RE: SQL code to check for records 365 prior to today

When dealing with dates, there are three things to to aware of.  One is that a year is not constant, can be 365 or 366.  Another thing to be aware of is time portion of a date.  You want the date and time as of midnight or is any time okay?  Lastly, is the Field1 part of an index?  So, using TRUNC on Field1 might not really be an option.  My solution, peculiar to Oracle, taking all three concerns into account is as follows:

WHERE Field1 <= ADD_MONTHS(TRUNC(SYSDATE), 12) + (1-1/24/60/60)

SELECT SYSDATE AS Today,
ADD_MONTHS(TRUNC(SYSDATE), 12) + (1-1/24/60/60) AS OneYear
FROM DUal

TODAY                ONEYEAR
-------------------- --------------------
22-JUL-2010 10:13:06 22-JUL-2011 23:59:59
 

RE: SQL code to check for records 365 prior to today

Quote:

ADD_MONTHS(TRUNC(SYSDATE), 12) + (1-1/24/60/60)
mine eyes, zey are bleedink!!  ze goggles, zey do nossink!!  dazed

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close