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 |
|
IDWY
Starting Member
6 Posts |
Posted - 2005-04-15 : 07:55:11
|
| I want to perform a query on a database which basically, takes the month number we're in now (April = 4) but then display records with from the previous 3 months...so that would be January, February and March. However i want this to run automatically so i dont need to change the code. So when the month numer ticks to MAY = 05, the query would then run February, March, April....Currently my database has records, which are grouped by monthy number e.g.MonthNumber Year Record Count11 2004 10112 2004 2501 2005 1002 2005 1053 2005 1104 2005 120ok....i have tried to use the getdate functionSELECT MONTH(GetDate()) ---> gives me the month number 4. Now is there any way i can use this to do what i want to do? Or does anyone have any other solutions...Hope this explains my problem....Thankyou...Dan |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-04-15 : 08:05:26
|
| Have you looked at DATEADD?SELECT MONTH(DATEADD(MM,-1,GETDATE()))SELECT MONTH(DATEADD(MM,-2,GETDATE()))SELECT MONTH(DATEADD(MM,-3,GETDATE()))MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
IDWY
Starting Member
6 Posts |
Posted - 2005-04-15 : 08:08:15
|
| Thats fantastic...i aint heard of that function. I've only been doin SQL about 10 months...hehe so still getting the grips of some functions...but thats great...thanx so much!!! Dab |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-04-15 : 08:13:42
|
| Go to Books Online and at the index tab type in "functions". You'll find all kinds of good stuff there. Also, look at "system functions".MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
|
|
|
|
|