Go Back   English Forum Switzerland > Off-Topic > Off-Topic > General off-topic
Reply
 
Thread Tools Display Modes
  #1  
Old 08.10.2007, 11:43
Cata1yst
Guest
 
Posts: n/a
SQL Help!

Hi all, I don't know if there's any SQL guru's here, but I have a nice problem.

I need to return the following from a where clause.....

In Simple English = (records within date range from today's date - 3 months)

I can't use a "between" as the dates are not static and the data is needed to populate a report on the fly, I've googled, read x3 books now and it's driving me mental.

Any ideas?
Reply With Quote
  #2  
Old 08.10.2007, 12:27
Member
 
Join Date: Oct 2007
Location: SZ
Posts: 162
Groaned at 1 Time in 1 Post
Thanked 66 Times in 37 Posts
dmarkd has no particular reputation at present
Re: SQL Help!

select * from yourtable
where datediff(m, YourdateColumn, Getdate()) < 3
Reply With Quote
  #3  
Old 08.10.2007, 12:34
smbuzby's Avatar
Senior Member
 
Join Date: Feb 2006
Location: Embrach North of Zurich City
Posts: 406
Groaned at 2 Times in 2 Posts
Thanked 102 Times in 63 Posts
Blog Entries: 6
smbuzby has earned some respectsmbuzby has earned some respect
Re: SQL Help!

Quote:
View Post
Hi all, I don't know if there's any SQL guru's here, but I have a nice problem.

I need to return the following from a where clause.....

In Simple English = (records within date range from today's date - 3 months)

I can't use a "between" as the dates are not static and the data is needed to populate a report on the fly, I've googled, read x3 books now and it's driving me mental.

Any ideas?
It really depends on what version of SQL and database server you have. e.g.

select * from myTable where theDate > sysdate -3; --will work on oracle (for 3 days)

select * from myTable where theDate > add_months(sysdate,-3); -- will work on oracle for months

Similar approach for sybase, but there you a dateadd function (months, -3) - can't remeber the exact syntax.

Last edited by smbuzby; 08.10.2007 at 12:42. Reason: oops noticed you wanted months not dats
Reply With Quote
  #4  
Old 08.10.2007, 12:43
Cata1yst
Guest
 
Posts: n/a
Re: SQL Help!

Quote:
View Post
It really depends on what version of SQL and database server you have. e.g.

select * from myTable where theDate > sysdate -3; --will work on oracle (for 3 days)

select * from myTable where theDate > add_months(sysdate,-3); -- will wokr on oracle for months
Similar approach for sybase, but there you a dateadd function (months, -3) - can't remeber the exact syntax.
Oracle 9.2i had a look at this seems to work, might post the context up in a mo, thanks guys!

I saw the add_months, couldn't work out the context probably being dumb!!

select distinct count (*) from sample where Changed_on > add_months(sysdate,-3)
Reply With Quote
  #5  
Old 08.10.2007, 12:44
smbuzby's Avatar
Senior Member
 
Join Date: Feb 2006
Location: Embrach North of Zurich City
Posts: 406
Groaned at 2 Times in 2 Posts
Thanked 102 Times in 63 Posts
Blog Entries: 6
smbuzby has earned some respectsmbuzby has earned some respect
Re: SQL Help!

Cheeky! It will work
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT +2. The time now is 19:01.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0