How can MSSQL SQL Server convert a date data to the format of “year month”?


Absrtact: in the SQL Server database, the method of converting date data to specified format is described below;

Experimental environment: sqlserver 2008 R2

Implementation ideas:

Implementation method 1: use the year function and month function to obtain the corresponding values, and then use string splicing method to output the corresponding data

Implementation method 2: convert (varchar (30), getdate(), 120), and then use string truncation function to intercept the first few data examples:

declare @maomao datetime
set @maomao =getdate()


select convert(varchar(4),year(@maomao))


Related reading:

MSSQL SQL Server common date conversion method sharing (convert datetime)

MSSQL sqlserver month keyword returns the integer of the specified date month – Usage

Introduction to the usage of MSSQL sqlserver year keyword

An example of MSSQL sqlserver string truncation function substring left right

Method sharing of “data table median” complemented by MSSqlServer

MSSQL sqlserver datepart date generation function instructions

MSSQL sqlserver datename date generation function instructions