Technical Article

Function to calculate Month End Date

,

This function will return you the month-end date. Input parameter may be any date of that month for which you want to calculate month-end.
Example:
Input: '2-20-2004'      Output: '2-29-2004'
Input: '2-15-2004'      Output: '2-29-2004'
Input: '3-20-2004'      Output: '3-31-2004'
Input: '3-30-2004'      Output: '3-31-2004'

/**********************************************************
Create By    : Hari Narayan Sharma [hari.bagra@gmail.com]
Creation Date: 17 Oct. 2006
Purpose     : To Get the Month End Date
How to Use   : Select dbo.GetMonthEnd('2-20-2004')
**********************************************************/
CREATE Function GetMonthEnd(@Date DateTime)
Returns DateTime
AS
Begin
Set @Date = DateAdd(Month,1,@Date)
Set @Date = Cast(Cast(Month(@Date)As varchar(2))+'-1-'+Cast(Year(@Date)As varchar(4)) As DateTime)
Return DateAdd(Day,-1,@Date)
End

Rate

3.2 (5)

You rated this post out of 5. Change rating

Share

Share

Rate

3.2 (5)

You rated this post out of 5. Change rating