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
how to get current datetime in SQL?
提问by 5YrsLaterDBA
Want to get current datetime
to insert into lastModifiedTime
column. I am using MySQL
database. My questions are:
想要current datetime
插入到lastModifiedTime
列中。我正在使用MySQL
数据库。我的问题是:
is there a function available in SQL? or
it is implementation depended so each database has its own function for this?
what is the function available in MySQL?
SQL中是否有可用的函数?或者
它取决于实现,所以每个数据库都有自己的功能吗?
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
I always just use NOW():
我总是只使用 NOW():
INSERT INTO table (lastModifiedTime) VALUES (NOW())
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_now
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_now
回答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
回答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
回答by Rob Farley
GETDATE()
orGETUTCDATE()
are now superseded by the richerSYSDATETIME
,SYSUTCDATETIME
, andSYSDATETIMEOFFSET
(in SQL 2008)- Yes, I don't think
ANSI
has ever declared anything, and so each manufacturer has their own. - That would be
NOW()
GETDATE()
或者GETUTCDATE()
现在被取代了更丰富SYSDATETIME
,SYSUTCDATETIME
和SYSDATETIMEOFFSET
(2008年SQL)- 是的,我认为
ANSI
从来没有声明过任何东西,所以每个制造商都有自己的。 - 那将是
NOW()
Hope this helps...
希望这可以帮助...
Rob
抢