How to get the number of days in the month (SQLServer 2005)

Create Function GetNumberOfDaysInTheMonth(@PDate DATETIME) Returns INT

AS

BEGIN 

RETURN CASE WHEN Month(@PDate) IN (1, 3, 5, 7, 8, 10, 12) THEN 31

                                WHEN Month(@PDate) IN (4, 6, 9, 11) THEN 30

                                WHEN Month(@PDate)=2 THEN

                    CASE  WHEN (Year(@PDate)%4 = 0)  AND  (Year(@PDate)%100 != 0  OR  Year(@PDate)%400 = 0)                     

                    THEN 29

                    ELSE 28

                    END

 END

END

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s