To get a number. days a month, we can directly use Day (), available in SQL.
Follow the link posted at the end of my answer for SQL Server 2005/2008.
The following example and result from SQL 2012
alter function dbo.[daysinm] ( @dates nvarchar(12) ) returns int as begin Declare @dates2 nvarchar(12) Declare @days int begin select @dates2 = (select DAY(EOMONTH(convert(datetime,@dates,103)))) set @days = convert(int,@dates2) end return @days end
Result in SSMS SQL Server
(no column name) 1 31
process:
When EOMONTH is used, depending on the date format we use, it is converted to the SQL Server DateTime format. Then the EOMONTH () date output will be 2016-12-31, having 2016 as Year, 12 as Month and 31 as Days. This output, when passed to Day (), gives you the total number of days in a month.
If we want to get an instant result for verification, we can directly run the code below,
select DAY(EOMONTH(convert(datetime,'08/12/2016',103)))
or
select DAY(EOMONTH(convert(datetime,getdate(),103)))
for links to work in SQL Server 2005/2008/2012, please follow the following external link ...
Find the number of days per month in SQL
VSV AdityaSarma Dec 08 '16 at 7:39 2016-12-08 07:39
source share