Skip to content
Advertisement

Extract last day of the month from MMM(M)YYYY

How can I extract the last day of the month from dates like

DEC2009, APR2013, AUG2008, NOV2017, JUL2014 etc.

I’ve tried datepart, convert, cast, month, year and I keep getting in a mess with type conversion errors.

Advertisement

Answer

Use try_convert and eomonth()

declare @date varchar(7)='jul2014'

select Day(EOMonth(Try_Convert(date, @date)))
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement