last day of the month function

10-21-2004, 12:22 AM
is there a function in sql that can return last day of the month?
with the parameter month and year. :(

Dennis DVR
10-21-2004, 02:13 AM
I don't know any built in function in SQL Server that gives the last day of a given month, but you can create your own function that return the last day of the month using the day and dateadd function of SQL Server.

here's a code sample that gives the last day of a given month

CREATE FUNCTION DaysInMonth(@Month as tinyint=1, @Year as int)
RETURNS tinyint AS
declare @d as datetime
declare @days as tinyint
set @d = cast(@month as varchar(2))+'/01/'+cast(@year as varchar(4))
set @days = day(dateadd(dd,-day(@d),dateadd(mm,1,@d)))
return @days

and to call the function

select dbo.daysinmonth(2,2002)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum