Tom Cooper
3/29/2007 4:47:00 PM
It uses an unsafe format for dates, is (at least to me) uneccessarily
complex and includes only midnight of the last day of the month. Use
something like
WHERE DETAIL_TYPE < 20
AND POST_DATE >= Select DateAdd(mm, DateDiff(mm, '20000101', GetDate()),
'19991201')
AND POST_DATE < Select DateAdd(mm, DateDiff(mm, '20000101', GetDate()),
'20000101')
Tom
"neheyen" <neheyen@discussions.microsoft.com> wrote in message
news:08451E67-B8E0-40C0-AA45-59580C97F0F7@microsoft.com...
>I have a need to find the data in the previous month. In other words, if
>ran
> today, the where clause should include the date range of February 1, 2007
> to
> February 28, 2007. It should be automatic so that I can include it in a
> scheduled DTS.
>
> A previous dba left this snippet, but no one knows if it was ever tested
> and
> it is beyond my understanding of SQL. Can someone take a look to see if it
> will work?
>
> WHERE (DETAIL_TYPE < 20)
>
> AND (POST_DATE >=
> CONVERT(datetime, CONVERT(varchar(2), MONTH(DATEADD(MM, - 1, GETDATE())))
> +
> '-' + CONVERT(varchar(2), DAY('01-dec-2000')) + '-' + CONVERT(varchar(4),
> YEAR(DATEADD(MM, 1, GETDATE())))) + 0)
>
> AND (POST_DATE <=
> CONVERT(datetime, CONVERT(varchar(2), MONTH(DATEADD(MM, 0, GETDATE()))) +
> '-' + CONVERT(varchar(2), DAY('01-dec-2000')) + '-' + CONVERT(varchar(4),
> YEAR(DATEADD(MM, 1, GETDATE())))) - 1)
>
> Thanks!
> Norman