Convert SQL Server Date to mm-yyyy

Prateek Daniels picture Prateek Daniels · Mar 3, 2015 · Viewed 70.6k times · Source

I'm trying to convert my Date which is (eg. 2012-04-20 05:54:59) format in into mm-yyyy. I came across some solutions that says you would need to convert into varchar . Is there any way using the Convert function ?

Thanks :)

Answer

Giorgos Betsos picture Giorgos Betsos · Mar 3, 2015

You can use FORMAT function, available from SQL Server 2012 onwards:

DECLARE @myDate DATETIME = '2012-04-20 05:54:59'
SELECT FORMAT(@myDate, 'MM-yyyy') 

Output:

04-2012