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
 Transact-SQL (2000)
 start date and end date of previous month

Author  Topic 

cbeganesh
Posting Yak Master

105 Posts

Posted - 2004-07-08 : 11:11:51
Does anybody have code to determine the start and end dates
of previous month
example, If i run the code today, i should get the start date as june 1st 2004 and end date as june 30th 2004
Thanks

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-07-08 : 11:15:38
Select
StartDate = dateadd(m,-1,convert(datetime,convert(nvarchar,getdate(),101))) - day(getdate())+1,
EndDate = convert(datetime,convert(nvarchar,getdate(),101)) - day(getdate())

Corey
Go to Top of Page

cbeganesh
Posting Yak Master

105 Posts

Posted - 2004-07-08 : 11:19:20
Thanks Corey, That was fast
Ganesh
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-07-08 : 11:55:50
You may not mean that your end date is "june 30th 2004" because that would be midnight between 29th and 30th. Which will be fine if your dates have NO time and you do
WHERE MyDate >= '01 Jun 2004 AND MyDate <= '30 Jun 2004'
but if MyDate has TIME as well then something AFTER midnight on the 29th/30th will be excluded, thus you may be better to phrase this as:
WHERE MyDate >= '01 Jun 2004 AND MyDate < '01 Jul 2004'
which is sure to also catch anything on 30th June that has a time

Kristen
Go to Top of Page
   

- Advertisement -