Mombu the Microsoft Forum sponsored links

Go Back   Mombu the Microsoft Forum > Microsoft > SQL Query Get Data a Month Old
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 23rd May 19:41
gabe matteson
External User
 
Posts: 1
Default SQL Query Get Data a Month Old



Does this part of the query look correct? It's formated so that it doesn't
use the time feature and zero's it out to midnight...

where datecreated between
DATEADD(m,-1,CAST(CONVERT(CHAR(10),GETDATE(),120)as DATETIME)) and GETDATE()
order by datecreated

how would i create a query that returns all the rows with the exception of
the past month....? thank you!
  Reply With Quote


  sponsored links


2 23rd May 19:41
dan guzman
External User
 
Posts: 1
Default SQL Query Get Data a Month Old



This looks OK to me as long as you want the last 28-31 days or so of data,
including the current day. However, you might consider using
DATEADD/DATEDIFF to remove the time portion of datetime:

DATEADD(day, 0, DATEDIFF(day, 0, GETDATE()))


If you want to return only data older than the last 28-31 days, use '<':

WHERE
datecreated <
DATEADD(m,-1,DATEADD(day, 0, DATEDIFF(day, 0, GETDATE()))), GETDATE()

To return only data older than the start of last month:

WHERE
datecreated <
DATEADD(month, DATEDIFF(month, 0, GETDATE()) - 1, 0)


--
Hope this helps.

Dan Guzman
SQL Server MVP
  Reply With Quote
3 23rd May 19:41
dan guzman
External User
 
Posts: 1
Default SQL Query Get Data a Month Old


Oops - copy/paste error. This should have been:

WHERE
datecreated <
DATEADD(m,-1,DATEADD(day, 0, DATEDIFF(day, 0, GETDATE())))

--
Hope this helps.

Dan Guzman
SQL Server MVP
  Reply With Quote
4 23rd May 19:41
gabe matteson
External User
 
Posts: 1
Default SQL Query Get Data a Month Old


Thank you, very helpful!
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666