4

I am having an issue while using GetDate(), for some reason is not returning the right time (it is 7 hours ahead from the actual time) I am using AZURE and the Database is configured with the right location (West US). I will appreciate any help!

I tried to run this script:

SELECT id,
       status,
       AcceptedDate,
       Getdate(),
       Datediff(hour, AcceptedDate, Getdate())
FROM   orderoffers
WHERE  status = 'Accepted' 
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
Carlos
  • 377
  • 5
  • 24
  • 1
    http://sqlblog.com/blogs/greg_low/archive/2013/01/20/windows-azure-sql-database-getdate-sysdatetime-sysutcdatetime-and-rounding.aspx – xQbert Dec 30 '14 at 19:14
  • To get local from UTC `SELECT GETDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'Central Standard Time'` – Don Cheadle Apr 24 '19 at 15:04

4 Answers4

9

Azure SQL Databases are always UTC, regardless of the data center. You'll want to handle time zone conversion at your application.

In this scenario, since you want to compare "now" to a data column, make sure AcceptedDate is also stored in UTC.

Reference

Joe Enos
  • 39,478
  • 11
  • 80
  • 136
3

The SQL databases on the Azure cloud are pegged against Greenwich Mean Time(GMT) or Coordinated Universal Time(UTC) however many applications are using DateTime.Now which is the time according to the regional settings specified on the host machine.

Sometimes this is not an issue when the DateTime is not used for any time spanning or comparisons and instead for display only. However if you migrate an existing Database to SQL Azure using the dates populated via GETDATE() or DateTime.Now you will have an offset, in your case it’s 7 hours during Daylight Saving Time or 8 hours during Standard Time.

John Pick
  • 5,562
  • 31
  • 31
Avijit
  • 1,219
  • 2
  • 15
  • 28
1

I created a simple function that returns the correct UK time whether in DST or not. It can be adapted for other time zones where DST kicks in.

CREATE FUNCTION [dbo].[f_CurrentDateTime]() RETURNS DATETIME AS
BEGIN
RETURN DATEADD(HOUR,CONVERT(INT,(SELECT is_currently_dst FROM sys.time_zone_info WHERE 1=1 AND NAME = 'GMT Standard Time')),GETDATE())
END
F. Müller
  • 3,969
  • 8
  • 38
  • 49
-1

In this modern times where infrastructure is scaled globally, it is good idea to save data in UTC and convert to a timezone based on users location preference.

Please refere: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.utcnow?view=netframework-4.7.2

Bharath
  • 115
  • 1
  • 8