Here is another function that you can provide the 2 pieces on information you have, Date and Time zone.
Convert to UTC
Nagesh CL
701
Reputation points
Hi All,
I have a sql table which contains the Date and the Timezone as below: -
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
Azure SQL Database
An Azure relational database service.
Answer accepted by question author
-
Alberto Morillo 35,406 Reputation points MVP Volunteer Moderator2021-08-25T23:56:23.613+00:00
1 additional answer
Sort by: Most helpful
-
Alberto Morillo 35,406 Reputation points MVP Volunteer Moderator2021-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 LocalDateBelow 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