gene gene - 4 months ago 8
SQL Question

SQL: How to get last day of the month using only month and year?

May be it is a silly question, but I want to find out how to find the last day of the month if I only have a year and a month?

I was thinking to use

EOMONTH()
, but this function needs the date consisted of year, month and day. I need to use only year and month.

How to do something like that?

Answer

If you are using Sql Server 2012 then I'd use DATEFROMPARTS.

DECLARE @year SMALLINT = 2016
    ,@month TINYINT= 02

SELECT EOMONTH(DATEFROMPARTS(@year,@month,1))
Comments