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)
 Get records from last month

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-05-08 : 09:50:29
Wes writes "Hi,

I need to run an SQL query to get all records from last month- i so far have this but it's not working:

LASTEDITED AS Date
FROM question
WHERE DATEDIFF(MONTH, LASTEDITED, GETDATE()) = -1

Can you shed any light?

Thanks in advance,

Wes."

timmy
Master Smack Fu Yak Hacker

1242 Posts

Posted - 2002-05-08 : 10:00:27
Wes,

DateDiff(month, date1, date2) will only give you the number of full months between your 2 dates. You could use a combination of the Year and Month functions as such:
SELECT LASTEDITED AS Date 
FROM question
WHERE Year(LASTEDITED) = Year(GETDATE())
AND Month(LASTEDITED) = Month(GETDATE())-1

You'll then need to add some extra smarts to allow for running it in January...

Tim


Go to Top of Page

YellowBug
Aged Yak Warrior

616 Posts

Posted - 2002-05-08 : 10:16:32
This should return everything in the last month.

SELECT *
FROM question
WHERE LASTEDITED BETWEEN DateAdd("mm", -1, getdate()) AND getdate()

Look for help on DateAdd
Go to Top of Page
   

- Advertisement -