dbTalk Databases Forums  

Last 3 months from now

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Last 3 months from now in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
valdemirs
 
Posts: n/a

Default Last 3 months from now - 03-02-2010 , 06:34 PM






Hi:
Please, I´m looking for a SQL query to get data of last 3 months.
(We are in March, 2 so I need a query to show me data from Dec/01/2009
until Feb/28/2010).

Is there any way to do this with SQL ?

Thanks.

Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Last 3 months from now - 03-02-2010 , 06:54 PM






The best would be to use a half-open interval:

SELECT <columns>
FROM Foo
WHERE datecol >= DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP) - 3, 0)
AND datecol < DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0);

--
Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
Reply




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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.