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

Time:2020-3-28

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 
LEFT(
CONVERT(varchar(20),@maomao,
120)
,7)

select convert(varchar(4),year(@maomao))
+'-'+
convert(varchar(2),month(@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