Friday, February 24, 2012

Dates

I'd like to create a query with a where clause that gets data for the
previous month, ie: when running the report in January the report would use
dates from 12/1 to 12/31.
How can this be done?Something like this?
/ ****************************************
*******************************
Determine the start and end of the previous month
****************************************
*******************************/
DECLARE @.dt datetime,
@.PrevMonthStart datetime,
@.PrevMonthEnd datetime
SELECT @.dt = getdate()
SELECT @.PrevMonthStart = dateadd(month,datediff(month,'1900',@.dt)
-1,'1900'),
@.PrevMonthEnd = dateadd(month,datediff(month,'1900',@.dt)
,'1900')-1
SELECT @.PrevMonthStart AS PreviousMonthStart, @.PrevMonthEnd AS
PreviousMonthEnd
Keith
"Jim" <Jim@.discussions.microsoft.com> wrote in message
news:DD304C3D-5BFD-460B-956B-37D0B4944D4B@.microsoft.com...
> I'd like to create a query with a where clause that gets data for the
> previous month, ie: when running the report in January the report would
use
> dates from 12/1 to 12/31.
> How can this be done?|||Thanks Keith, it worked.
"Keith Kratochvil" wrote:

> Something like this?
>
> / ****************************************
*******************************
> Determine the start and end of the previous month
> ****************************************
*******************************/
> DECLARE @.dt datetime,
> @.PrevMonthStart datetime,
> @.PrevMonthEnd datetime
> SELECT @.dt = getdate()
> SELECT @.PrevMonthStart = dateadd(month,datediff(month,'1900',@.dt)
-1,'1900'
),
> @.PrevMonthEnd = dateadd(month,datediff(month,'1900',@.dt)
,'1900')-1
> SELECT @.PrevMonthStart AS PreviousMonthStart, @.PrevMonthEnd AS
> PreviousMonthEnd
>
> --
> Keith
>
> "Jim" <Jim@.discussions.microsoft.com> wrote in message
> news:DD304C3D-5BFD-460B-956B-37D0B4944D4B@.microsoft.com...
> use
>

No comments:

Post a Comment