Home > database >  Remove timezone in SQL Server query while keeping the precision
Remove timezone in SQL Server query while keeping the precision

Time:09-28

I am running the following query on SQL Server 2019:

SELECT
    autoid,
    ReceivedUTC,
    ReceivedUTC AT TIME ZONE 'UTC' AT TIME ZONE 'Central Europe Standard Time' AS ReceivedTST
FROM 
    "EPOMCAFEE_Events".dbo."EPOEvents" t

From which I get the following (sample) results:

autoid   ReceivedUTC              ReceivedTST
----------------------------------------------------------------
606416  2022-09-26 16:36:53.960   2022-09-26 18:36:53.960  02:00
606415  2022-09-26 16:36:53.960   2022-09-26 18:36:53.960  02:00

How can I remove the timezone part (" 02:00"), while keeping the precision (".960"), in my column ReceivedTST ?

Best regards

Altin

CodePudding user response:

One way to remove the offset is to CAST the datetimeoffset value to datetime2(3):

SELECT
    autoid,
    ReceivedUTC,
    CAST(ReceivedUTC AT TIME ZONE 'UTC' AT TIME ZONE 'Central Europe Standard Time' AS datetime2(3)) AS ReceivedTST
FROM 
    "EPOMCAFEE_Events".dbo."EPOEvents" t;
  • Related