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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Displaying records in a table by month number

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 Count
11 2004 101
12 2004 250
1 2005 100
2 2005 105
3 2005 110
4 2005 120

ok....i have tried to use the getdate function


SELECT 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()))


MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

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
Go to Top of Page

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".

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page
   

- Advertisement -