;

How to Get the Current Time Zone of the Server in SQL Server.


Tutorialsrack 05/11/2021 SQL Server

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.

Example 1: Using CURRENT_TIMEZONE() Function

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”.

Here is an example to get the current timezone of the Server in SQL Server.

Example 1: Using CURRENT_TIMEZONE() Function
SELECT CURRENT_TIMEZONE() as 'Current TimeZone';
Output

Current TimeZone

-------------------------------------------------------

(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi

 

(1 row affected)

Example2: Earlier Versions of SQL Server such as 2017 or Lower

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.

Example2: Earlier Versions of SQL Server such as 2017 or Lower
DECLARE @TimeZone VARCHAR(50);
EXEC MASTER.dbo.xp_regread 'HKEY_LOCAL_MACHINE',
'SYSTEM\CurrentControlSet\Control\TimeZoneInformation',
'TimeZoneKeyName',@TimeZone OUT;
SELECT @TimeZone as 'Current TimeZone';
Output

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!


Related Posts



Comments

Recent Posts
Tags