Convert to UTC

Nagesh CL 686 Reputation points
2021-08-24T14:18:43.783+00:00

Hi All,
I have a sql table which contains the Date and the Timezone as below: -

126020-image.png

Is there a way in SQL to convert the OrderDate to UTC timezone based on the TimeZone field?

Thanks.

Regards,
Nagesh CL

Azure SQL Database
0 comments No comments
{count} vote

Accepted answer
  1. Alberto Morillo 34,151 Reputation points MVP
    2021-08-25T23:56:23.613+00:00

    Here is another function that you can provide the 2 pieces on information you have, Date and Time zone.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Alberto Morillo 34,151 Reputation points MVP
    2021-08-24T15:20:29.543+00:00

    Please see the recommended options like AT TIME ZONE on this article.

    Here you will find more examples of how to use AT TIME ZONE.

    You may want to use a function like the one below:

    CREATE FUNCTION [dbo].[fnGetLocalDate] (@DateToConvert DATETIMEOFFSET = NULL)   
    RETURNS TABLE   
    AS   
        RETURN   
          SELECT CONVERT(DATETIMEOFFSET, @DateToConvert AT TIME ZONE   
                                         (SELECT Value   
                                          FROM   LookUp.Config   
                                          WHERE  Property = 'TimeZone')) AS   
                        LocalDate   
    

    Below an example of how to use it.

    SELECT o.name, L.LocalDate  
    FROM sys.objects o  
    CROSS APPLY [dbo].[fnGetLocalDate](o.modify_date) AS L  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.