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)
 return start date and enddate of previous month

Author  Topic 

jung1975
Aged Yak Warrior

503 Posts

Posted - 2005-03-16 : 20:42:52
I am trying to create a function that will return the start date and end date of previous month based on the input date.

For example, If I put '03/16/05', it will return '02/01/05' as the start date and '02/28/05' as the end date.

Please help!!!



robvolk
Most Valuable Yak

15732 Posts

Posted - 2005-03-16 : 20:49:08
SELECT DateAdd(month, DateDiff(month, 0, dateValue)-1, 0) as StartOfMonth,
DateAdd(day, -1, DateAdd(month, DateDiff(month, 0, dateValue), 0)) as EndOfMonth
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-03-17 : 04:42:18
Do you want the last day of the month, or the first day of the next month?

If you use it along the lines of

WHERE MyDate >= '01-Feb-2005' AND MyDate <= '28-Feb-2005'

then you will miss any value of MyDate column that has a TIME as weel as bing on the 28-Feb-2005

So I would favour changing this to

WHERE MyDate >= '01-Feb-2005' AND MyDate < '01-Mar-2005'

and thus want my function to return the 1st of the month following as the upper limit.

But maybe you just want to display the 1st/last day of the month in a report, in which case you're all sorted!

Kristen
Go to Top of Page
   

- Advertisement -