SQL 将日期转换为 YYYYMM 格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14217751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:01:54  来源:igfitidea点击:

Convert date to YYYYMM format

sqlsql-servertsql

提问by loviji

I want to select value = 201301

我想选择值 = 201301

select getdate(), cast(datepart(year, getdate()) as varchar(4))+cast(datepart(MONTH, getdate()) as varchar(2))

it returns 20131

它返回 20131

what is the normal way to do this?

这样做的正常方法是什么?

回答by Hamlet Hakobyan

SELECT CONVERT(nvarchar(6), GETDATE(), 112)

回答by Hogan

SELECT LEFT(CONVERT(varchar, GetDate(),112),6)

回答by D-Shih

I know it is an old topic, but If your SQL serverversion is higher than 2012.

我知道这是一个老话题,但是如果您的SQL server版本高于 2012。

There is another simple option can choose, FORMATfunction.

还有一个简单的选项可以选择,FORMAT功能。

SELECT FORMAT(GetDate(),'yyyyMM')

sqlfiddle

sqlfiddle

回答by Luaan

Actually, this isthe proper way to get what you want, unless you can use MS SQL 2014 (which finally enables custom format strings for date times).

实际上,这获得所需内容的正确方法,除非您可以使用 MS SQL 2014(最终启用日期时间的自定义格式字符串)。

To get yyyymminstead of yyyym, you can use this little trick:

yyyymm代替yyyym,您可以使用这个小技巧:

select 
 right('0000' + cast(datepart(year, getdate()) as varchar(4)), 4)
 + right('00' + cast(datepart(month, getdate()) as varchar(2)), 2)

It's faster and more reliable than gettings parts of convert(..., 112).

它比 .gets 的部分更快、更可靠convert(..., 112)

回答by Ema.H

You can convert your date in many formats, for example :

您可以将日期转换为多种格式,例如:

CONVERT(NVARCHAR(10), DATE_OF_DAY, 103) => 15/09/2016
CONVERT(NVARCHAR(10), DATE_OF_DAY, 3) => 15/09/16

Syntaxe :

语法:

CONVERT('TheTypeYouWant', 'TheDateToConvert', 'TheCodeForFormating' * )
  • The code is an integer, here 3 is the third formating without century, if you want the century just change the code to 103.
  • 代码是一个整数,这里的 3 是没有世纪的第三个格式,如果你想要世纪,只需将代码更改为 103。

In your case, i've just converted and restrict size by nvarchar(6) like this :

在您的情况下,我刚刚通过 nvarchar(6) 转换和限制大小,如下所示:

CONVERT(NVARCHAR(6), DATE_OF_DAY, 112) => 201609

See more at : http://www.w3schools.com/sql/func_convert.asp

查看更多信息:http: //www.w3schools.com/sql/func_convert.asp

回答by Bill Stidham

A more efficient method, that uses integer math rather than strings/varchars, that will result in an int type rather than a string type is:

一种更有效的方法,它使用整数数学而不是字符串/varchars,这将导致 int 类型而不是字符串类型:

SELECT YYYYMM = (YEAR(GETDATE()) * 100) + MONTH(GETDATE())

Adds two zeros to the right side of the year and then adds the month to the added two zeros.

在年份的右侧添加两个零,然后将月份添加到添加的两个零上。

回答by Marc B

It's month 1, so you're getting an expected value. you'll have to zeropad the month (1 -> 01), as per this answer: How do I convert an int to a zero padded string in T-SQL?

现在是第 1 个月,因此您获得了预期值。您必须按照以下答案对月份进行零填充(1 -> 01):如何在 T-SQL 中将 int 转换为零填充字符串?