SwitchOffset in SQL Server

The SWITCHOFFSET function returns a datetimeoffset value that has been altered from the stored time zone offset to a new time zone offset that has been given.

Syntax: SWITCHOFFSET ( datetimeoffset_expression, timezoneoffset_expression )

Example:
SELECT SWITCHOFFSET (GETDATE(), ‘-04:00’)
Returns: 2022-02-20 09:16:10.993 -04:00

The following workaround, on the other hand, takes the same amount of time. However the output format is different. It doesn’t include the offset.

SELECT DATEADD(HOUR,-4, GETDATE())
Returns: 2022-02-20 09:16:10.993

Hope this helps.

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s