Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I select time back to 24 months in query? 1

Status
Not open for further replies.

kate8

Programmer
Feb 14, 2001
184
0
0
US
Hi,

I have a query that need to be run every month. The time range in the query is from today’s date go back 24 month. For example, if I run it on 11/01/2012, then it will look like this.
d.timestamp >= {ts '2010-11-01 00:00:01'} AND d.timestamp <{ts '2012-11-01 00:00:01'})
Now I have to manually change it every time I run it. I am wondering if I can set it up in the query. I can use GETDATE()to replace {ts '2012-11-01 00:00:01'}, but how can I get the time back to 24 month.

Thanks a lot for any suggestions and helps!!
 
Kate,

Use the DATEADD(datepart, number, date) with a negative value for the month as date part, i.e.
SQL:
SELECT dateadd(mm, -24, getdate())

MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA)
 
Thank you so much!!
I knew I should use dateadd(), but was not sure how to do it. Thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top