Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
mshsilver
Posting Yak Master
112 Posts |
Posted - 2010-01-04 : 06:08:49
|
Hi,I want to write a query from a single table that shows results based on the date the query is run and the previous two months only. The todays date need s to be dynamic and can't be hard coded which is where i need a little help. I have seen date functions but can't see how to apply them in the query to say today - two months. Any advice would be a great start to the New Year. Thanksselect * from wce_history where recordedtime >= "two months ago from date query is run" |
|
|
Buzzard724
Yak Posting Veteran
66 Posts |
Posted - 2010-01-04 : 06:26:12
|
| TrySELECT * from wce_history WHERE recordedtime >= DateAdd(M,-2,GetDate())--DateAdd() takes paraemters to say what is added (or substracted) from what - in this case Months -2 from GetDate()--GetDate() returns the system date and time |
 |
|
|
mshsilver
Posting Yak Master
112 Posts |
Posted - 2010-01-04 : 06:33:19
|
I got the answer to this. Hope it helps someone else.select * from wce_history where recordedtime >= dateadd(mm,-2,getdate()) dateadd is used to calculate date offsets based on a given date. Getdate() is current date, current time. Please see BOL (Books Online, the SQL Server help system installed together with SQL Server) for details. |
 |
|
|
mshsilver
Posting Yak Master
112 Posts |
Posted - 2010-01-04 : 06:34:08
|
| Thanks Buzzard, as i posted my reply yours appeared. Great help thanks |
 |
|
|
|
|
|