SQL Server 相当于 MySQL 的 NOW()?

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

SQL Server equivalent of MySQL's NOW()?

sqlsql-server

提问by Andrew G. Johnson

I'm a MySQL guy working on a SQL Server project, trying to get a datetime field to show the current time. In MySQL I'd use NOW() but it isn't accepting that.

我是一个从事 SQL Server 项目的 MySQL 工作人员,试图获取一个日期时间字段来显示当前时间。在 MySQL 中,我会使用 NOW() 但它不接受。

INSERT INTO timelog (datetime_filed) VALUES (NOW())

回答by Daniel Schaffer

getdate()or getutcdate().

getdate()getutcdate()

回答by Steven A. Lowe

getdate() 

is the direct equivalent, but you should always use UTC datetimes

是直接等效的,但您应该始终使用 UTC 日期时间

getutcdate()

whether your app operates across timezones or not - otherwise you run the risk of screwing up date math at the spring/fall transitions

您的应用是否跨时区运行 - 否则您将面临在春季/秋季过渡时搞砸日期数学的风险

回答by DMK

SYSDATETIME()and SYSUTCDATETIME()

SYSDATETIME()SYSUTCDATETIME()

are the DateTime2equivalents of

DateTime2 的等价物

GetDate()and GetUTCDate()

GetDate()GetUTCDate()

which return a DateTime.

返回一个DateTime

DateTime2is now the preferred method for storing the date and time in SQL Server 2008+. See the following StackOverflow Post.

DateTime2现在是在 SQL Server 2008+ 中存储日期和时间的首选方法。请参阅以下StackOverflow 帖子

回答by Ian Varley

You can also use CURRENT_TIMESTAMP, if you feel like being more ANSI compliant (though if you're porting code between database vendors, that'll be the least of your worries). It's exactly the same as GetDate()under the covers (see this questionfor more on that).

CURRENT_TIMESTAMP如果您觉得更符合 ANSI 标准,您也可以使用(尽管如果您在数据库供应商之间移植代码,那将是您最不担心的)。它GetDate()与封面完全相同(有关更多信息,请参阅此问题)。

There's no ANSI equivalent for GetUTCDate(), however, which is probably the one you should be using if your app operates in more than a single time zone ...

GetUTCDate()但是,没有 ANSI 等效项,如果您的应用程序在多个时区中运行,这可能是您应该使用的...