Following User Defined Function (UDF) returns the numbers of days in month. It is very simple yet very powerful and full proof UDF.
CREATE FUNCTION [dbo].[udf_GetNumDaysInMonth] ( @myDateTime DATETIME )
RETURNS INT
AS
BEGIN
DECLARE @rtDate INT
SET @rtDate = CASE WHEN MONTH(@myDateTime)
IN (1, 3, 5, 7, 8, 10, 12) THEN 31
WHEN MONTH(@myDateTime) IN (4, 6, 9, 11) THEN 30
ELSE CASE WHEN (YEAR(@myDateTime) % 4 = 0
AND
YEAR(@myDateTime) % 100 != 0)
OR
(YEAR(@myDateTime) % 400 = 0)
THEN 29
ELSE 28 END
END
RETURN @rtDate
END
GO
Run following script in Query Editor:
SELECT dbo.udf_GetNumDaysInMonth(GETDATE()) NumDaysInMonth
GO
ResultSet:
NumDaysInMonth
———————–
31
Reference : Pinal Dave (http://blog.SQLAuthority.com)




You really don’t need to do the math, you can just get the last day of the month using built in date functions and assign that to your return variable. This is built in for you so you don’t have to worry about leap years or anything
CREATE FUNCTION [dbo].[udf_GetNumDaysInMonth] ( @myDateTime DATETIME )
RETURNS INT
AS
BEGIN
DECLARE @rtDate INT
SET @rtDate = DATEPART(dd, DATEADD(ms,-3,DATEADD(mm, DATEDIFF(m,0,@myDateTime)+1, 0)))
RETURN @rtDate
END
GO
Thanks Simon,
I have used similar logic many times. I just decided to do something different. You can check other datetime related articles here.
http://blog.sqlauthority.com/tag/sql-datetime/
Regards,
Pinal Dave ( http://www.SQLAuthority.com )
Hello, Expert
I’m using Transact-Query, how I select a particular month out of in different years in the database?
The format column field 2005/08/26…….
thanks you
ryan,
Hi This User defined function will return the no. of days in a month
CREATE FUNCTION [dbo].[ufn_GetDaysInMonth](
@CurrentDate DATETIME )
RETURNS INT
AS
BEGIN
DECLARE @ReturnDays INT
SET @ReturnDays =
CASE WHEN MONTH(@CurrentDate) IN (1, 3, 5, 7, 8, 10, 12) THEN 31
WHEN MONTH(@CurrentDate) IN (4, 6, 9, 11) THEN 30
ELSE
CASE WHEN (YEAR(@CurrentDate) % 4 = 0 AND YEAR(@CurrentDate) % 100 != 0) OR (YEAR(@CurrentDate) % 400 = 0) THEN 29
ELSE 28
END
END
RETURN @ReturnDays
END
–SELECT dbo.ufn_GetDaysInMonth(GETDATE()) No_Of_Days_In_Month
GO
Try this one-liner:
CREATE FUNCTION [dbo].[ufn_GetDaysInMonth](
@CurrentDate DATETIME )
RETURNS INT
AS
BEGIN
RETURN DAY(DATEADD(d, -DAY(DATEADD(m,1,@dt)),DATEADD(m,1,@dt)))
END
Hi pinaldave,
Thank you very much
Just a modification to Doug’s posting :
CREATE FUNCTION [dbo].[ufn_GetDaysInMonth] (@CurrentDate DATETIME )
RETURNS INT
AS
BEGIN
DECLARE @RetDate INT
SET @RetDate = DAY(DATEADD(d, -DAY(DATEADD(m,1,@CurrentDate)),DATEADD(m,1,@CurrentDate)))
RETURN @RetDate
END
– SELECT [dbo].[ufn_GetDaysInMonth] (’2008/02/25′)
EXCELLENT …:))
EXACTLY SUITS MY REQUIREMENT
SUPERB
THNX A LOT………
We have already some built in functions avail, then why should we go behind all those steps…
Anyway, good logic…
-Mahesh
I want No.of days in a Month – Here i will give Month(2) and Year(2009),
so, there are two parameters.
Pls Give me the function
[...] SQL SERVER – UDF – User Defined Function – Get Number of Days in Month [...]
[...] A Multi-Statement Table-Value user-defined function returns a table and is also an exceptional alternative to a view as the function can support multiple T-SQL statements to build the final result where the view is limited to a single SELECT statement. Also, the ability to pass parameters into a TSQL select command or a group of them gives us the capability to in essence create a parameterized, non-updateable view of the data in the underlying tables. Within the create function command you must define the table structure that is being returned. After creating this type of user-defined function, It can be used in the FROM clause of a T-SQL command unlike the behavior found when using a stored procedure which can also return record sets. (Read Here For Example) [...]
hi.. experts… i need a similar UDF…
my req is…. if i give 3 param’s to the UDF.. like..
“month,year and dayofweek” then i should get a list of that day fom the given month,year
ex. for April 2009 Sunday
UDFDays(@mm,@yy,@dyw)
i.e UDFDays(04,2009,’Sunday’)
then i should get list as..
6
13
20
27
i.e… the dates from the month given for weekday.. like for Apil 2009 Saturday..
i expect..esult as
5
12
19
26
etc…
thanks in advance…
Re post:… slight mistake frm my side…
hi.. experts… i need a similar UDF…
my req is…. if i give 3 param’s to the UDF.. like..
“month,year and dayofweek” then i should get a list of that day fom the given month,year
ex. for April 2009 Sunday
UDFDays(@mm,@yy,@dyw)
i.e UDFDays(04,2009,’Sunday’)
then i should get list as..
5
12
19
26
i.e… the dates from the month given for weekday.. like for Apil 2009 Saturday..
i expect..esult as
4
11
18
25
etc…
thanks in advance…
@gourav, here’s a quick shot:
CREATE FUNCTION UDFDays(@mm tinyint,@yy smallint,@dyw varchar(9))
RETURNS TABLE
AS
RETURN
WITH
Data(The_Date)
AS
(
SELECT CONVERT(smalldatetime, CAST(@mm as varchar(2)) + ‘/01/’ + CAST(@YY as varchar(4)) , 101)
),
Days_In_Month
AS
(
SELECT The_Date, DATEADD(d, -1, DATEADD(m, 1, The_Date)) Next_Month FROM Data
UNION ALL
SELECT DATEADD(d, 1, The_Date), Next_Month FROM Days_In_Month WHERE The_Date < Next_Month
)
SELECT
DATEPART(d, The_Date) Day_Number
FROM
Days_In_Month
WHERE
DATENAME(dw, The_Date) = @dyw;
GO
thanks bro… you hav done it….thank you very very much…
Regards
Gourav