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

时间:2022-11-05 11:45:08

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_date is set to something like this:

每当创建一个新行时,order_date的数据设置为如下所示:

Apr 8 2014 9:52AM

2014年4月8日上午9:52

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

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

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.

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

2 个解决方案

#1


4  

You should consider using DATETIMEOFFSET as your daatype instead of DATETIME.

您应该考虑使用DATETIMEOFFSET作为您的daatype而不是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

#2


16  

Use 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.

如果在一小时后设置时间,则可能是系统时间错误。因此,如果您只是要求服务器管理员更正它会更好。

#1


4  

You should consider using DATETIMEOFFSET as your daatype instead of DATETIME.

您应该考虑使用DATETIMEOFFSET作为您的daatype而不是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

#2


16  

Use 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.

如果在一小时后设置时间,则可能是系统时间错误。因此,如果您只是要求服务器管理员更正它会更好。