SQL 如何在SQL中获取当前日期时间?

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

how to get current datetime in SQL?

sqldatabasecurrent-time

提问by 5YrsLaterDBA

Want to get current datetimeto insert into lastModifiedTimecolumn. I am using MySQLdatabase. My questions are:

想要current datetime插入到lastModifiedTime列中。我正在使用MySQL数据库。我的问题是:

  1. is there a function available in SQL? or

  2. it is implementation depended so each database has its own function for this?

  3. what is the function available in MySQL?

  1. SQL中是否有可用的函数?或者

  2. 它取决于实现,所以每个数据库都有自己的功能吗?

  3. MySQL 中可用的功能是什么?

回答by Eduardo Molteni

Complete answer:

完整答案:

1. Is there a function available in SQL?
Yes, the SQL 92 spec, Oct 97, pg. 171, section 6.16 specifies this functions:

1. SQL 中是否有可用的函数?
是的,SQL 92 规范,10 月 97 日,第。第 171 条第 6.16 节规定了此功能:

CURRENT_TIME       Time of day at moment of evaluation
CURRENT_DATE       Date at moment of evaluation
CURRENT_TIMESTAMP  Date & Time at moment of evaluation

2. It is implementation depended so each database has its own function for this?
Each database has its own implementations, but they have to implement the three function above if they comply with the SQL 92 specification (but depends on the version of the spec)

2.它取决于实现,所以每个数据库都有自己的功能?
每个数据库都有自己的实现,但如果符合SQL 92规范,则必须实现上述三个功能(但取决于规范的版本)

3. What is the function available in MySQL?

3. MySQL 有哪些功能?

NOW() returns 2009-08-05 15:13:00  
CURDATE() returns 2009-08-05  
CURTIME() returns 15:13:00  

(As SixFootTallRabbit says)

(正如 SixFootTallRabbit 所说)

回答by Nathan Loding

回答by Joseph Mansfield

NOW()returns 2009-08-05 15:13:00

NOW()回报 2009-08-05 15:13:00

CURDATE()returns 2009-08-05

CURDATE()回报 2009-08-05

CURTIME()returns 15:13:00

CURTIME()回报 15:13:00

回答by Matthew Jones

I want my datetime, and I want it now()!

我想要我的约会时间,我想要它now()

For MySQL, anyway.

无论如何,对于 MySQL。

回答by Naive

SYSDATETIME() 2007-04-30 13:10:02.0474381
SYSDATETIMEOFFSET()2007-04-30 13:10:02.0474381 -07:00
SYSUTCDATETIME() 2007-04-30 20:10:02.0474381
CURRENT_TIMESTAMP 2007-04-30 13:10:02.047 +
GETDATE() 2007-04-30 13:10:02.047 
GETUTCDATE() 2007-04-30 20:10:02.047

I guess NOW() doesn't work sometime and gives error 'NOW' is not a recognized built-in function name.

我猜 NOW() 有时不起作用,并给出错误“NOW”不是公认的内置函数名称。

Hope it helps!!! Thank You. https://docs.microsoft.com/en-us/sql/t-sql/functions/getdate-transact-sql

希望能帮助到你!!!谢谢你。 https://docs.microsoft.com/en-us/sql/t-sql/functions/getdate-transact-sql

回答by Saif Khan

For SQL Server use GetDate() or current_timestamp. You can format the result with the Convert(dataType,value,format). Tag your question with the correct Database Server.

对于 SQL Server,使用 GetDate() 或 current_timestamp。您可以使用 Convert(dataType,value,format) 格式化结果。使用正确的数据库服务器标记您的问题。

回答by pygalaxy

Just an add on for SQLite you can also use

只是 SQLite 的附加组件,您也可以使用

CURRENT_TIME
CURRENT_DATE
CURRENT_TIMESTAMP

for the current time, current date and current timestamp. You will get the same results as for SQL

当前时间、当前日期和当前时间戳。您将获得与 SQL 相同的结果

sqlsqlite

sql sqlite

回答by Rob Farley

  1. GETDATE()or GETUTCDATE()are now superseded by the richer SYSDATETIME, SYSUTCDATETIME, and SYSDATETIMEOFFSET(in SQL 2008)
  2. Yes, I don't think ANSIhas ever declared anything, and so each manufacturer has their own.
  3. That would be NOW()
  1. GETDATE()或者GETUTCDATE()现在被取代了更丰富SYSDATETIMESYSUTCDATETIMESYSDATETIMEOFFSET(2008年SQL)
  2. 是的,我认为ANSI从来没有声明过任何东西,所以每个制造商都有自己的。
  3. 那将是 NOW()

Hope this helps...

希望这可以帮助...

Rob