Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GETUTCDATE Function

Tags:

sql

sql-server

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

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

like image 292
Emma Thapa Avatar asked Feb 21 '13 06:02

Emma Thapa


People also ask

What is the difference between Getdate and Getutcdate?

The difference between GETDATE() and GETUTCDATE() is in timezone, the GETDATE() function returns the current date and time in the local timezone, the timezone where your database server is running, but GETUTCDATE() return the current time and date in UTC (Universal Time Coordinate) or GMT timezone.

How do I store UTC datetime in SQL Server?

Inserting a UTC datetime into a datetimeoffset column If you're using GETUTCDATE() to get the UTC datetime, it won't have the timezone offset appended to it. But if you're inserting into a datetimeoffset column, then it will put the UTC timezone offset for you. It automatically appended the +00:00 timezone offset.

How get current time in SQL query?

SQL Server GETDATE() Function The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss.

How do you find the UTC date and time?

JavaScript Date getUTCDate() getUTCDate() returns the day of the month (1 to 31) of a date object. getUTCDate() returns the day according to UTC.


1 Answers

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

  • It is a nondeterministic function, Views and expressions that reference this column cannot be indexed.

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.

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

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.

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.
like image 126
Vishal Suthar Avatar answered Sep 23 '22 08:09

Vishal Suthar