SQL Server – How to Format Month with Two Digits

querysql server

how can i make monthid column values appear as example: 201901, not 20191.

INSERT INTO [dbo].[table2](accountno, monthid, active)
SELECT accountno, CONCAT(YEAR(InsertionDate), month(insertiondate)), active 
FROM [dbo].[table1]

Best Answer

DECLARE @t TABLE(InsertionDate datetime);
INSERT @t(InsertionDate) VALUES(GETDATE()), ('20190125');

SELECT CONVERT(char(6), InsertionDate, 112) FROM @t;

Result:

202004
201901