Earlier I wrote about SQL SERVER – Datetime Function SWITCHOFFSET Example. After reading this blog post, I got another quick reply that if I can explain the usage of TODATETIMEOFFSET as well.
Let us go over the definition of the TODATETIMEOFFSET from BOL: Returns a datetimeoffset value that is translated from a datetime2 expression.
What essentially it does is that changes the current offset only offset which we defined. Let us see the example of the same.
SELECT SYSDATETIMEOFFSET() GetCurrentOffSet; SELECT TODATETIMEOFFSET(SYSDATETIMEOFFSET(), '-04:00') 'GetCurrentOffSet-4'; SELECT TODATETIMEOFFSET(SYSDATETIMEOFFSET(), '-02:00') 'GetCurrentOffSet-2'; SELECT TODATETIMEOFFSET(SYSDATETIMEOFFSET(), '+00:00') 'GetCurrentOffSet+0'; SELECT TODATETIMEOFFSET(SYSDATETIMEOFFSET(), '+02:00') 'GetCurrentOffSet+2'; SELECT TODATETIMEOFFSET(SYSDATETIMEOFFSET(), '+04:00') 'GetCurrentOffSet+4';
Let us see the resultset below.
It is quite clear from the example that only offset changes in the time, but unlike SWITCHOFFSET the datetime remains the same. Datetime datatype stores date and time part. If you have only date value, You can add time, part to a datetime variable using direct literal value and + operator.
In this SQL in Sixty Seconds video we cover two different methods to display the datetime in specific format. 1) CONVERT function and 2) FORMAT function.
Let me know what you think of this video in the comment section.
Reference: Pinal Dave (https://blog.sqlauthority.com)