Topic Status: Some information in this topic is preview and subject to change in future releases. Preview information describes new features or changes to existing features in Microsoft SQL Server 2016 Community Technology Preview 2 (CTP2).
Returns a datetimeoffset value that is changed from the stored time zone offset to a specified new time zone offset.
For an overview of all Transact-SQL date and time data types and functions, see Date and Time Data Types and Functions (Transact-SQL).
Applies to: SQL Server (SQL Server 2008 through current version), Azure SQL Database, Azure SQL Data Warehouse Public Preview.
Use SWITCHOFFSET to select a datetimeoffset value into a time zone offset that is different from the time zone offset that was originally stored. SWITCHOFFSET does not update the stored time_zone value.
SWITCHOFFSET can be used to update a datetimeoffset column.
Using SWITCHOFFSET with the function GETDATE() can cause the query to run slowly. This is because the query optimizer is unable to obtain accurate cardinality estimates for the datetime value. To resolve this problem, use the OPTION (RECOMPILE) query hint to force the query optimizer to recompile a query plan the next time the same query is executed. The optimizer will then have accurate cardinality estimates and will produce a more efficient query plan. For more information about the RECOMPILE query hint, see Query Hints (Transact-SQL).
DECLARE @dt datetimeoffset = switchoffset (CONVERT(datetimeoffset, GETDATE()), '-04:00'); SELECT * FROM t WHERE c1 > @dt OPTION (RECOMPILE);
The following example uses SWITCHOFFSET to display a different time zone offset than the value stored in the database.
CREATE TABLE dbo.test ( ColDatetimeoffset datetimeoffset ); GO INSERT INTO dbo.test VALUES ('1998-09-20 7:45:50.71345 -5:00'); GO SELECT SWITCHOFFSET (ColDatetimeoffset, '-08:00') FROM dbo.test; GO --Returns: 1998-09-20 04:45:50.7134500 -08:00 SELECT ColDatetimeoffset FROM dbo.test; --Returns: 1998-09-20 07:45:50.7134500 -05:00