T-SQL How to Create a new column as INT from Datetime column as YYMM or YYYYMM
You can add a computed column. Example:




create table docs
( doc_id int not null primary key,
  dt datetime not null
) ;



✓






insert into docs (doc_id, dt)
values
(1, '2000-01-01 00:00:00'),
(2, '2001-05-12 12:30:00'),
(3, '2007-07-07 16:45:00'),
(4, '2018-01-01 12:00:00'),
(5, '2018-05-15 15:50:00') ;



5 rows affected






select * from docs;



doc_id | dt                 
-----: | :------------------
     1 | 01/01/2000 00:00:00
     2 | 12/05/2001 12:30:00
     3 | 07/07/2007 16:45:00
     4 | 01/01/2018 12:00:00
     5 | 15/05/2018 15:50:00






alter table docs
add year_month as year(dt) * 100 + month(dt) ;






select * from docs;



doc_id | dt                  | year_month
-----: | :------------------ | ---------:
     1 | 01/01/2000 00:00:00 |     200001
     2 | 12/05/2001 12:30:00 |     200105
     3 | 07/07/2007 16:45:00 |     200707
     4 | 01/01/2018 12:00:00 |     201801
     5 | 15/05/2018 15:50:00 |     201805



db&lt;>fiddle here