如何向日期时间 SQL 列数据添加 1 小时?

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

How do I add 1 hour to datetime SQL column data?

sqlsql-serverdatetimegetdatehour

提问by Kelsey

I used this part of a query to create a table column for the date and time a row is added:

我使用查询的这一部分为添加行的日期和时间创建了一个表列:

order_date datetime NOT NULL DEFAULT GETDATE()

order_date datetime NOT NULL DEFAULT GETDATE()

and whenever a new row is created, the data for order_dateis set to something like this:

每当创建新行时,数据order_date都会设置为如下所示:

Apr 8 2014 9:52AM

Apr 8 2014 9:52AM

For some reason, when a row is created and the order_datecolumn data is set, the hour is set 1 hour back. For example, the above column data for Apr 8 2014 9:52AMwas set at 10:52AM.

出于某种原因,当创建一行并设置order_date列数据时,小时设置为 1 小时。例如,上面的列数据Apr 8 2014 9:52AM设置为上午 10:52。

Is there a way to set it 1 hour ahead so that it is correct with my current time?

有没有办法将它设置为提前 1 小时,以便它与我当前的时间正确?

Thank you for any help. All help is greatly appreciated.

感谢您的任何帮助。非常感谢所有帮助。

采纳答案by Karl Kieninger

You should consider using DATETIMEOFFSETas your daatype instead of DATETIME.

您应该考虑使用DATETIMEOFFSET作为数据类型而不是 DATETIME。

Defines a date that is combined with a time of a day that has time zone awareness and is based on a 24-hour clock.

定义一个日期,该日期与具有时区意识并基于 24 小时制的一天中的时间相结合。

You can use it with SYSDATETIMEOFFSET().

您可以将它与SYSDATETIMEOFFSET() 一起使用。

Returns a datetimeoffset(7) value that contains the date and time of the computer on which the instance of SQL Server is running. The time zone offset is included.

Example:

例子:

 CREATE TABLE DateTest (id INT, order_date DATETIMEOFFSET NOT NULL DEFAULT SYSDATETIMEOFFSET())
 INSERT INTO DateTest (id) VALUES (1)
 SELECT * FROM DateTest

回答by Nathan

Use DATEADD()

使用 DATEADD()

DATEADD(hh, 1, order_date)

EDIT:

编辑:

If the time is being set an hour back, you may have a wrong system time. So, it would be better if you just ask server admin to correct it.

如果时间被设置为一小时后,您可能有错误的系统时间。所以,如果你只是要求服务器管理员纠正它会更好。