Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure SQL Database - How to change time zone settings?

Tags:

timezone

azure

My Azure server is hosted in East US. In the SQL DB, when I use getdate(), it returns UTC time. But I need to get EST time. How can I achieve it? Is there any setting I need to change?

like image 854
Iniyavan Avatar asked Sep 15 '25 01:09

Iniyavan


2 Answers

The easiest way to get the current time in the US Eastern time zone is:

select SYSDATETIMEOFFSET() AT TIME ZONE 'Eastern Standard Time'

This returns a datetimeoffset and accounts correctly for daylight saving time.

like image 138
Matt Johnson-Pint Avatar answered Sep 17 '25 18:09

Matt Johnson-Pint


OP has developed a function:

FN_GET_EST(GETDATE()). 

CREATE FUNCTION FN_GET_EST(@p_in_date as datetime) returns DATETIME 
as 
begin 
DECLARE @dt_offset AS datetimeoffset 
SET @dt_offset = CONVERT(datetimeoffset, @p_in_date) AT TIME ZONE 'Eastern Standard Time' 
RETURN CONVERT(datetime, @dt_offset); 
end
like image 38
Joseph Xu Avatar answered Sep 17 '25 19:09

Joseph Xu