February 11, 2005 at 9:13 am
What's the best way to query a date field to get the only the past 60 days from the current date? I want to run this every day. Newbie here so please be gentle. Thanks!
February 11, 2005 at 9:18 am
Better yet, here is what I have and I just need to alter it to take only the past 60 days. Thanks.
DECLARE @MTH INT
DECLARE @YR INT
DECLARE @BEGDATE DATETIME
DECLARE @ENDDATE DATETIME
SELECT @mth =
case
when datepart(month,getdate())=1 then 12
else
datepart(month,getdate())-1
end
SELECT @yr =
case
when datepart(month,getdate())=1 then datepart(year,getdate())-1
else
datepart(year,getdate())
end
SELECT @BegDate = convert(varchar,@mth)+ '/01/'+ convert(varchar,@yr)
SELECT @EndDate = case
when @mth in (1,3,5,7,8,10,12) then convert(varchar,@mth)+ '/31/'+ convert(varchar,@yr)
when @mth in (4, 6, 9, 11) then convert(varchar,@mth) + '/30/' + convert(varchar,@yr)
when @mth = 2 and (@yr % 4 = 0) then convert(varchar,@mth)+ '/29/'+ convert(varchar,@yr)
when @mth = 2 and (@yr % 4 <> 0) then convert(varchar,@mth)+ '/28/'+ convert(varchar,@yr)
end
February 11, 2005 at 9:18 am
See BOL on the DATEDIFF() function.
Where DateDiff(dd, YourDateColumn, GetDate() ) <= 60
February 14, 2005 at 2:17 am
The lazycoder might use something like
WHERE Your datecolumn >= GETDATE() - 60
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply