SQL Azure time convert

get local time in Azure SQL

SQL function 1 (it is work)

// SQL timezone convert function

CREATE OR ALTER FUNCTION dbo.CurrentLocalTime
( 
    @TimeZoneName sysname
)
RETURNS datetime2
AS
BEGIN
 
--
-- Test examples: 
/*
SELECT dbo.CurrentLocalTime('AUS Eastern Standard Time');
*/
 
  RETURN DATEADD(hour, 
                 TRY_CAST((SELECT REPLACE(current_utc_offset, N':', N'.')
                           FROM sys.time_zone_info
                           WHERE [name] = @TimeZoneName) AS decimal(18,2)), 
                 SYSDATETIME());
END;
GO


select GETDATE() AS SystemTime, dbo.CurrentLocalTime('AUS Eastern Standard Time') as LocalTime

function 2

Last updated

Was this helpful?