SQL Server CURRENT_TIMEZONE_ID: How to Use It
In this article
SQL Server does not have a built-in function called CURRENT_TIMEZONE_ID. The correct function is CURRENT_TIMEZONE_ID() (with parentheses) and it returns the ID of the current time zone as an integer. If you need the time zone name, use CURRENT_TIMEZONE(). This guide shows you how to call the function, what it returns, and how to use it in queries and stored procedures.
What You'll Need
- SQL Server 2016 or later (the function was introduced in SQL Server 2016).
- Access to a database with permission to run SELECT statements.
- A SQL client such as SQL Server Management Studio (SSMS), Azure Data Studio, or sqlcmd.
Step-by-Step
- Connect to your SQL Server instance using your preferred client. For example, open SSMS and connect to the database engine.
- Open a new query window (in SSMS: File > New > Query with Current Connection, or press Ctrl+N).
- Run the function by executing the following statement:
The result is an integer that represents the time zone ID. For example, on a server set to UTC, it returns 0. On a server set to Pacific Standard Time, it might return 2 (the exact value depends on the server's time zone configuration).SELECT CURRENT_TIMEZONE_ID() AS TimeZoneID; - Compare with the time zone name by also running:
This returns the time zone name as a string, such as "(UTC) Coordinated Universal Time" or "(UTC-08:00) Pacific Time (US & Canada)".SELECT CURRENT_TIMEZONE() AS TimeZoneName; - Use the function in a query to filter or display data. For example, to show the current time zone ID alongside other session information:
(Note: SESSION_CONTEXT requires SQL Server 2016 or later and may return NULL if not set.)SELECT SESSION_CONTEXT(N'user_name') AS UserName, CURRENT_TIMEZONE_ID() AS TimeZoneID, CURRENT_TIMEZONE() AS TimeZoneName; - Store the result in a variable if you need to use it in procedural code:
DECLARE @tzID INT = CURRENT_TIMEZONE_ID(); SELECT @tzID AS TimeZoneID; - Use it in a stored procedure or function to log the time zone of the session. For example:
This captures the time zone at the moment of execution.CREATE PROCEDURE LogSessionTimeZone AS BEGIN INSERT INTO SessionLog (LogTime, TimeZoneID, TimeZoneName) VALUES (SYSDATETIMEOFFSET(), CURRENT_TIMEZONE_ID(), CURRENT_TIMEZONE()); END;
Common Mistakes
- Using
CURRENT_TIMEZONE_IDwithout parentheses: SQL Server requires parentheses for functions, soCURRENT_TIMEZONE_IDalone will cause a syntax error. Always writeCURRENT_TIMEZONE_ID(). - Expecting a time zone name: The function returns an integer ID, not a name. Use
CURRENT_TIMEZONE()for the name. - Assuming the ID is universal: The ID is an internal identifier that can change if the server's time zone is changed. Do not hard-code it in application logic; use it only for display or logging.
- Running on older SQL Server versions: The function is not available in SQL Server 2014 or earlier. If you get an error, check your version with
SELECT @@VERSION;. - Confusing with
SYSDATETIMEOFFSET(): That function returns the current date and time with offset, not the time zone ID.
Tips
- Use
CURRENT_TIMEZONE_ID()together withCURRENT_TIMEZONE()to get both the ID and the name for logging or auditing. - If you need the offset from UTC, use
DATEPART(TZOFFSET, SYSDATETIMEOFFSET())orSYSDATETIMEOFFSET()directly. - Remember that the function returns the time zone of the server or the session, not the client. It reflects the server's configured time zone.
- When storing time zone information, consider storing the name (from
CURRENT_TIMEZONE()) rather than the ID, as names are more stable and readable. - Test the function in a development environment before using it in production to understand the values returned by your server.
Troubleshooting
If you encounter issues, try these steps:
- Error: 'CURRENT_TIMEZONE_ID' is not a recognized built-in function name. This usually means you are running an older version of SQL Server (pre-2016) or you omitted the parentheses. Check your version and syntax.
- Function returns NULL: This should not happen; if it does, ensure you are connected to a SQL Server instance and not a different database engine.
- Unexpected ID value: The ID is based on the server's time zone setting. To see the current time zone, run
SELECT CURRENT_TIMEZONE();. You can change the server time zone in the operating system settings, but that requires a restart of SQL Server. - Permission issues: The function is a built-in scalar function and does not require special permissions beyond the ability to run a query.
- Client tool does not recognize the function: Update your SQL client to a version that supports SQL Server 2016 or later, such as SSMS 18 or later.
Frequently Asked Questions
What does CURRENT_TIMEZONE_ID() return?
It returns an integer that represents the ID of the current time zone of the SQL Server instance.
How do I get the time zone name instead of the ID?
Use the CURRENT_TIMEZONE() function, which returns a string with the time zone name.
Is CURRENT_TIMEZONE_ID() available in Azure SQL Database?
Yes, it is available in Azure SQL Database and Azure SQL Managed Instance, as well as SQL Server 2016 and later.
Can I use CURRENT_TIMEZONE_ID() in a WHERE clause?
Yes, you can use it in any expression, but note that it returns the same value for the entire query unless the time zone changes mid-query (which is unlikely).
What is the difference between CURRENT_TIMEZONE_ID() and SYSDATETIMEOFFSET()?
CURRENT_TIMEZONE_ID() returns the time zone ID, while SYSDATETIMEOFFSET() returns the current date and time with the time zone offset.
Does CURRENT_TIMEZONE_ID() require parentheses?
Yes, like all functions in SQL Server, it must be called with parentheses: CURRENT_TIMEZONE_ID().