In this article, you will learn how to get the current timezone of the server in SQL Server. A very common question we developers often encounter is what is the current Timezone of the server where the SQL Server is installed. Here are the two examples to get the current timezone of the server in SQL Server.
In SQL Server 2019, Microsoft introduces the CURRENT_TIMEZONE()
function for returning the time zone of the server. This function “returns the name of the time zone observed by a server or an instance”.
UTC
and the CURRENT_TIMEZONE()
function returns the name of the UTC time zone.Here is an example to get the current timezone of the Server in SQL Server.
SELECT CURRENT_TIMEZONE() as 'Current TimeZone';
Current TimeZone
-------------------------------------------------------
(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi
(1 row affected)
Earlier versions of SQL Server do not support the CURRENT_TIMEZONE()
function. Here is the simple script to get the current time of the server in SQL Server.
DECLARE @TimeZone VARCHAR(50);
EXEC MASTER.dbo.xp_regread 'HKEY_LOCAL_MACHINE',
'SYSTEM\CurrentControlSet\Control\TimeZoneInformation',
'TimeZoneKeyName',@TimeZone OUT;
SELECT @TimeZone as 'Current TimeZone';
Current TimeZone
--------------------------------------------------
India Standard Time
(1 row affected)
I hope this article will help you to understand how to get the current timezone of the server in SQL Server.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments