Search

Monday, April 2, 2012

Find the number of days in a month

With the help of built in SQL Server functions we can easily achieve this as shown below.

Select Day(DateAdd(Month, 1, '01/01/2012') - Day(DateAdd(Month, 1, '02/01/2012')))
Generalized Solution:

We can generalize it by creating a "User defined Stored Procedure" as shown below:

Create Function DaysInMonth (@dtDate datetime) returns int
as
Begin
Return(Select Day(DateAdd(Month, 1, @dtDate) - Day(DateAdd(Month, 1, @dtDate))))
End
Go


No comments:

Post a Comment