SQL:如何从日期时间字段中提取月份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15254419/
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 14:06:14  来源:igfitidea点击:

SQL: how to extract the month from a date time field

sqlsql-server

提问by mechalaris

I need to to extract the month from date time fieldA then put the result into fieldB as an integer in the same table IF fieldB is empty.

我需要从日期时间字段A中提取月份,然后将结果作为同一个表中的整数放入字段B,如果字段B为空。

eg) 1988-02-03 00:00:00.000I need to extract 02 and put the result into another field only if that field is empty.

例如)1988-02-03 00:00:00.000只有当该字段为空时,我才需要提取 02 并将结果放入另一个字段。

I guess this will end up as a stored procedure.

我想这最终会成为一个存储过程。

回答by juergen d

Use MONTH()

使用MONTH()

UPDATE your_table
SET some_column = MONTH(date_column)
WHERE some_column IS NULL

回答by Infinite Possibilities

MONTH(fieldA)or DATEPART(M, fieldA)

MONTH(fieldA)或者 DATEPART(M, fieldA)

回答by Matt Busche

SQL Server has a built-in function called month()that will return the month for date and timestamp data types

SQL Server 有一个内置函数month(),该函数将返回日期和时间戳数据类型的月份

UPDATE yourtable
SET newcolumn = month(columnname)
WHERE newcolumn IS NULL