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
SQL: how to extract the month from a date time field
提问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.000
I 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
回答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