May 24, 2006 at 12:14 pm
I need to do rolling date select of the previous 18 months of data, not including the current month. For example: 11/30/2004 through 04/30/2006. Any help would be appreciated.
Thank you
May 24, 2006 at 12:41 pm
Take a look at the datediff function in Books Online...I think that will do what you need.
Michelle
May 24, 2006 at 1:41 pm
I was hoping for a detailed example. I would think this is common for automated jobs.
May 24, 2006 at 1:57 pm
May 24, 2006 at 2:23 pm
This helps, but I don't see how this gets me the data for only the previous 18 full months not including the current month. In other words 11/01/2004 through 04/30/2006. I meant 11/01, not 11/30.
Thank you
May 24, 2006 at 5:09 pm
There may be an easier way, but this works....
select
dateadd( day, 0, cast( datepart ( month, dateadd( month, -18, getdate() ) ) as varchar(2) ) +
'/01/' + cast( datepart( year, getdate() ) as varchar(4) ) ),
dateadd( day, -1, cast( datepart ( month, getdate() ) as varchar(2) ) +
'/01/' + cast( datepart ( year, getdate() ) as varchar(4) ) )
/* if the date you are comparing against includes time, you'll need to
subtract one second from the current month */
select
convert( datetime, cast( datepart ( month, dateadd( month, -18, getdate() ) ) as varchar(2) ) +
'/01/' + cast( datepart( year, getdate() ) as varchar(4) ), 101),
dateadd ( second, -1, convert( datetime, cast( datepart ( month, getdate() ) as varchar(2) ) +
'/01/' + cast( datepart( year, getdate() ) as varchar(4) ), 101) )
I would suggest using a function.
Hope this helps.
______________________________________________________________________
Personal Motto: Why push the envelope when you can just open it?
If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.
Jason L. SelburgMay 25, 2006 at 12:16 am
where datecol >= dateadd(month, datediff(month, 0, getdate()) - 18, 0)
and datecol < dateadd(month, datediff(month, 0, getdate()), 0)
May 25, 2006 at 2:59 am
May 25, 2006 at 6:51 am
I knew there was an easier way [KH] ....
______________________________________________________________________
Personal Motto: Why push the envelope when you can just open it?
If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.
Jason L. SelburgViewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply