Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL GetDate() returns wrong time

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' 
like image 343
Carlos Avatar asked Sep 11 '25 19:09

Carlos


2 Answers

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

like image 68
Joe Enos Avatar answered Sep 13 '25 13:09

Joe Enos


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.

like image 33
Avijit Avatar answered Sep 13 '25 12:09

Avijit