Friday, February 17, 2012

Date calculations

I'm trying to write a query to select all data for the last 3 months
from a table. I want to be able to put it into a stored procedure and
schedule it to run once a month. So since I want it to be automated I
don't want to pass it start and end date every time it runs. I'm so not
good with using the date functions. Can anyone help out'
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!SELECT columns FROM table WHERE datetimeColumn >= DATEADD(MONTH, -3,
GETDATE())
If you want to go on specific date / midnight boundaries, see
http://www.aspfaq.com/2444 for some shortcuts.
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"Rachael" <rachael_faber@.hotmail.com> wrote in message
news:ui22exSEEHA.580@.TK2MSFTNGP11.phx.gbl...
> I'm trying to write a query to select all data for the last 3 months
> from a table. I want to be able to put it into a stored procedure and
> schedule it to run once a month. So since I want it to be automated I
> don't want to pass it start and end date every time it runs. I'm so not
> good with using the date functions. Can anyone help out'
>
> *** Sent via Developersdex http://www.examnotes.net ***
> Don't just participate in USENET...get rewarded for it!|||You can use the DATEADD() function adding minus three months from the
current datetime, for which you use the CURRENT_TIMESTAMP function.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
"Rachael" <rachael_faber@.hotmail.com> wrote in message
news:ui22exSEEHA.580@.TK2MSFTNGP11.phx.gbl...
> I'm trying to write a query to select all data for the last 3 months
> from a table. I want to be able to put it into a stored procedure and
> schedule it to run once a month. So since I want it to be automated I
> don't want to pass it start and end date every time it runs. I'm so not
> good with using the date functions. Can anyone help out'
>
> *** Sent via Developersdex http://www.examnotes.net ***
> Don't just participate in USENET...get rewarded for it!

No comments:

Post a Comment