If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Need help with CURRENT_DATE and INTERVAL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-14-04, 14:25
etishler etishler is offline
Registered User
 
Join Date: Jun 2004
Location: New Haven, CT USA
Posts: 4
Need help with CURRENT_DATE and INTERVAL

I am faily new to SQL and I am trying to construct a query that returns all records with a specified date field is 1 year old or more.

I have been rummaging through all the online SQL references, tutorials, etc for the last frew hours and all I have found is CURRENT_DATE which returns a DATE value for the current system date. I have also found refernences to the INTERVAL function, which I believe can specify date or time intervals.

I think perhaps that these two applied together could perhaps provide an expression I could use in a where clause. but I cannot figure out the syntax, or even if I am on the right track.

Basically what I want to accomplish is

SELECT * FROM TABLE
WHERE TABLE.DATEFIELD < CURRENT_DATE-(1 YEAR)

But I do not know how to do this.

Any help is greatly appreciated

Thanks,

Eric
Reply With Quote
  #2 (permalink)  
Old 07-14-04, 16:15
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Cool

Did you try:
Code:
SELECT * FROM TABLE
WHERE TABLE.DATEFIELD < CURRENT_DATE-365
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #3 (permalink)  
Old 07-14-04, 16:23
etishler etishler is offline
Registered User
 
Join Date: Jun 2004
Location: New Haven, CT USA
Posts: 4
I could try that. But how would it know I meant 365 days and not 365 minutes or hours for that matter.

Actually, I was able to accomplish what I wanted with the following

Code:
SELECT * FROM TABLE
WHERE TABLE.DATEFIELD <  '07/14/2003';
This works for today. But what if I wanted to run this everyday without having to manually change the query? There has to be a way to automate this ...
Reply With Quote
  #4 (permalink)  
Old 07-15-04, 05:54
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
How would you know? Execute
SELECT datefield, datefield - 365 last_year
FROM table;
and you'll see the result.

Oracle interprets number constants in arithmetic date expressions as number of days. For example, SYSDATE + 1 is tomorrow. SYSDATE + (10/1440) is ten minutes from now.

However, not all years have 365 days (Feb 29th, right?).

There's a function named ADD_MONTHS in Oracle. Your example would then be
SELECT * FROM table
WHERE datefield < ADD_MONTHS(sysdate, -12);

Is there something similar in SQL version you use?
Reply With Quote
  #5 (permalink)  
Old 07-15-04, 07:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
if you are using microsoft sql server

SELECT * FROM TABLE
WHERE TABLE.DATEFIELD < dateadd(yyyy,-1,getdate())

if you are using microsoft access

SELECT * FROM TABLE
WHERE TABLE.DATEFIELD < dateadd("yyyy",-1,date())
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 07-15-04, 09:07
etishler etishler is offline
Registered User
 
Join Date: Jun 2004
Location: New Haven, CT USA
Posts: 4
Sorry guys, I am using Firebird 1.0.3. The only function mentioned that recently became available in Firebird 1.5 is ADD_MONTHS, which, of course, I cannot use until I upgrade. (Not a viable option at thiis time)

However a special thanks to LKBrwn_DBA (and littlefoot with your follow up) since the syntax:

CURRENT_DATE-365 did indeed work.

Sometime the simplest solution is the best

Thanks!

Eric
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On