all groups > sql server misc > march 2005 >
You're in the

sql server misc

group:

sql get next 30 days



sql get next 30 days Maziar Aflatoun
3/29/2005 9:27:05 AM
sql server misc: Hi everyone,

I have a date field in my database. I like to issue a sql query to return
all the rows for the next 30 days from today. Can anyone help?

Thank you
Maz.

Re: sql get next 30 days webjunk NO[at]SPAM dfmartin.com
3/29/2005 12:02:11 PM
You can try this:
------------------------
Declare @future datetime
Set @future = getdate() + 30

Select * from mytable where datefield >= getdate() - 1 AND datefield >=
@future
-----------------

Note the '-1' after getdate(). It's needed to include items with today
as their day. This is because time is a factor and getdate == right
now, this instant! So that part depends on how the dates are getting
in there to begin with. Often times folks are entering a date and not
a time so it goes to something like 12:00:00. A variation on the where
clause might be :

where datediff(day, note_date, @temp) < 31 (again the 31 is to include
items that have today as their day)

Hope this helps.
David Martin
Re: sql get next 30 days webjunk NO[at]SPAM dfmartin.com
3/29/2005 12:05:00 PM
Oops. That first where clause has a backwards sign. It should be:

Select * from mytable where datefield >= getdate() - 1 AND datefield <=
@future

the second >= should have been <=

-David
AddThis Social Bookmark Button