SQL GETUTCDATE 函数

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

GETUTCDATE Function

sqlsql-server

提问by Emma Thapa

Why should we use GETUTCDATE()function instead of getdate()or vice-verse?

为什么我们应该使用GETUTCDATE()函数而不是getdate()反之亦然?

What is the purpose of both? Please help me with example.

两者的目的是什么?请帮我举例。

回答by Vishal Suthar

GETUTCDATE()represents the current UTCtime (Universal Time Coordinate or Greenwich Mean Time)

GETUTCDATE()代表当前UTC时间(世界时坐标或格林威治标准时间)

  • It is a nondeterministicfunction, Views and expressions that reference this column cannot be indexed.
  • 它是一个nondeterministic函数,无法索引引用此列的视图和表达式。

The current UTC time is derived from the current local time and the time zone setting in the operating system of the computer on which SQL Server is running.

当前 UTC 时间源自当前本地时间和运行 SQL Server 的计算机的操作系统中的时区设置。

The difference between GETDATE()and GETUTCDATE()is time zone number of the SQL Server machine.

GETDATE()和之间的区别GETUTCDATE()SQL Server 机器的区号

For better understanding see the example:

为了更好地理解,请参见示例:

DECLARE @local_time DATETIME;
DECLARE @gmt_time DATETIME;
SET @local_time = GETDATE();
SET @gmt_time = GETUTCDATE();
SELECT 'Server local time: '
   + CONVERT(VARCHAR(40),@local_time);
SELECT 'Server GMT time: '
   + CONVERT(VARCHAR(40),@gmt_time);
SELECT 'Server time zone: '
   + CONVERT(VARCHAR(40),
      DATEDIFF(hour,@gmt_time,@local_time));
GO

OUTPUT:

输出:

Server local time: Jun  2 2007 10:06PM
Server GMT time: Jun  2 2007  4:21PM
Server time zone: 6

GETDATE ()

获取日期 ()

  • It will return the date with your regional time or we can say, it returns the current system date and time.
  • If you are connected to the SQL server remotely then the timestamp displayed will be the timestamp of the remote machine and not your local machine.
  • It is a nondeterministic function.
  • Views and expressions that reference this column cannot be indexed.
  • 它将返回带有您所在地区时间的日期,或者我们可以说,它返回当前系统日期和时间。
  • 如果您远程连接到 SQL 服务器,则显示的时间戳将是远程机器的时间戳,而不是您本地机器的时间戳。
  • 它是一个非确定性函数。
  • 无法索引引用此列的视图和表达式。

GETUTCDATE ()

获取日期 ()

  • It will return the date and GMT time (Greenwich Mean Time).
  • GETUTCDATE () can be used to store the timestamp that is independent of Time Zones.
  • The current UTC time is derived from the current local time and the time zone setting in the operating system of the computer on which the instance of Microsoft SQL Server is running.
  • GETUTCDATE is a nondeterministic function.
  • Views and expressions that reference this column cannot be indexed.
  • 它将返回日期和 GMT 时间(格林威治标准时间)。
  • GETUTCDATE() 可用于存储独立于时区的时间戳。
  • 当前 UTC 时间源自当前本地时间和运行 Microsoft SQL Server 实例的计算机的操作系统中的时区设置。
  • GETUTCDATE 是一个不确定的函数。
  • 无法索引引用此列的视图和表达式。

回答by Tayyab Amin

getdate() will return the date as is on your local system. Let's say you are pulling some data and the DB server is located on a UTC location. your date time columns will return UTC time stamps and you won't be able to match time stamps.

getdate() 将返回本地系统上的日期。假设您正在提取一些数据并且数据库服务器位于 UTC 位置。您的日期时间列将返回 UTC 时间戳,您将无法匹配时间戳。

So you can adjust the date time by using datediff function on UTC date and getdate.

因此,您可以通过在 UTC 日期和 getdate 上使用 datediff 函数来调整日期时间。