Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL code to check for records 365 prior to today

Status
Not open for further replies.

Kruzer

Programmer
Jun 16, 2000
117
0
0
US
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top