简体   繁体   中英

Convert CURRENT_TIMESTAMP from UTC to EST

I am trying to declare a datetime variable at the beginning of my query that holds the run time of the query. I am pulling UTC time but I need EST.

I have tried the code below:

DECLARE @RunDate as datetimeoffset  
SET @RunDate = CURRENT_TIMESTAMP at time zone 'Eastern Standard Time'  

DECLARE @RunDateEST as datetime  
SET @RunDateEST = CONVERT(DATETIME, SWITCHOFFSET(@RunDate, DATEPART(tz, SYSDATETIMEOFFSET())))  

SELECT @RunDate as runDate, @RunDateEST as runDateEST  

But receive the following:

Actual Result (adds 4 hours to UTC time):  
    runDate:       2019-07-31 13:34:01.2770000 -04:00  
    runDateEST:    2019-07-31 17:34:01.277  

Does anyone know what my query is missing that will achieve the following result?

Expected Result (subtract 4 hours from UTC time):  
    runDate:        2019-07-31 13:34:01.2770000 -04:00  
    runDateEST:     2019-07-31 09:34:01.277  

You can try the below:

DECLARE @RunDateUTC as datetimeoffset  
SET @RunDateUTC = CURRENT_TIMESTAMP AT TIME ZONE 'UTC' 

DECLARE @RunDateEST as datetime  

SET @RunDateEST = CONVERT(DATETIME, @RunDateUTC AT TIME ZONE 'Eastern Standard Time')

SELECT @RunDateUTC,@RunDateEST

Short Version

Use a single :

select cast(SYSDATETIMEOFFSET() at time zone 'Eastern Standard Time' as datetime)

Explanation

The problem is using CURRENT_TIMESTAMP in the first place. CURRENT_TIMESTAMP is equivalent to GETDATE() . It returns a local datetime without any offset indication. This means that

select  CURRENT_TIMESTAMP at time zone 'Eastern Standard Time'

is equivalent to

select  GETDATE() at time zone 'Eastern Standard Time'

In this case, AT TIME ZONE simply appends the offset without making any conversions. It can't do any because it has no idea what the source offset is. This is clearly explained in the documentation :

Converts an inputdate to the corresponding datetimeoffset value in the target time zone. When inputdate is provided without offset information, the function applies the offset of the time zone assuming that inputdate is in the target time zone. If inputdate is provided as a datetimeoffset value, then AT TIME ZONE clause converts it into the target time zone using the time zone conversion rules.

I suspect the server's timezone is UTC, which means that SWITCHOFFSET has to add 4 hours to the EST time to get the proper UTC time.

To fix this, use SYSDATETIMEOFFSET() instead of GETDATE/CURRENT_TIMESTAMP :

select SYSDATETIMEOFFSET() at time zone 'Eastern Standard Time' 

This returns :

2019-08-01 07:05:23.5447068 -04:00

To strip the offset, cast the result to a datetime :

select cast(SYSDATETIMEOFFSET() at time zone 'Eastern Standard Time' as datetime)

This returns :

2019-08-01 07:05:23.543

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM