T-SQL: Group timestamp by Year/Month

You can group datetime fields by year/month by using a simple ‘format’. Of course, there are other possibilities for doing this. See example below:

  declare @format varchar(100) = 'yyyy/MM'
   SELECT  format(timestamp,@format), count(*)
  FROM [MY_DATABASE].[dbo].[MY_TABLE]
  group by format(timestamp,@format)
  order by format(timestamp,@format) desc;
Share your love