- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - SYSDATETIMEOFFSET() Function
The SYSDATETIMEOFFSET() function returns a value of DATETIMEOFFSET(7) that provides the current system date and time, which also includes the time zone, of the computer on which the SQL Server instance is executing.
The SYSDATETIMEOFFSET function returns a datetime2 data type with the format yyyy-mm-dd hh:mm:ss.nnnnnn.
Syntax
Following is the syntax of the SQL SYSDATETIMEOFFSET() function −
SYSDATETIMEOFFSET()
Parameters
This function does not accept any parameters.
Example
Let us retrieve the sysdatetimeoffset by using the following query −
SELECT SYSDATETIMEOFFSET() AS Result;
Output
When we execute the above query, the output is obtained as follows −
+------------------------------------+ | Result | +------------------------------------+ | 2023-02-17 16:23:11.4906580 +05:30 | +------------------------------------+
Example
In the following example, we are going to use DATEPART() function to retrieve the offset of time zone, which returns an integer that represents the offset of time zone in minutes by running the following query −
SELECT SYSDATETIMEOFFSET() AS 'Date and Time', DATEPART(TZoffset, SYSDATETIMEOFFSET()) AS 'TimeZone Offset';
Output
On executing the above query, the output is displayed as follows −
+------------------------------------+-----------------+ | Date and Time | TimeZone Offset | +------------------------------------+-----------------+ | 2023-02-17 16:30:19.6822375 +05:30 | 330 | +------------------------------------+-----------------+
Example
Here, we are using FORMAT() function to retrieve the offset of time zone in a string specifically, we can also use the zz, z, and zzz arguments to get the return value in required format.
Let us look into the example, where we are using the FORMAT() function to return the offset of time zone by using the following query −
SELECT SYSDATETIMEOFFSET() AS 'Date and Time', FORMAT(SYSDATETIMEOFFSET(), 'zz') AS 'zz';
Output
The output for the above query is produced as given below −
+------------------------------------+-----+ | Date and Time | zz | +------------------------------------------+ | 2023-02-17 16:30:19.6822375 +05:30 | +05 | +------------------------------------+-----+
Example
Here we are using the CONVERT() function for converting the return value into other data types.
Look at the following example, where we are going to use the CONVERT() function and converting the return value into date values by using the following query −
SELECT CONVERT (DATE, SYSDATETIMEOFFSET()) AS 'Date';
Output
If we execute the above query, the result is produced as follows −
+------------+ | Date | +------------+ | 2023-02-17 | +------------+
Example
In this example, we are using the DATEDIFF() function along with the SYSDATETIMEOFFSET() function to find the differences in minutes and seconds between the current datetime and variable −
DECLARE @DATETIME DATETIME2 SET @DATETIME = '2023-02-17 05:40:10.3021234' SELECT 'Minutes_Difference' AS 'MINUTE', DATEDIFF(MINUTE, @DATETIME, SYSDATETIMEOFFSET()) AS NumberofMinutes; SELECT 'Seconds_Difference' AS 'SECOND', DATEDIFF(SECOND, @DATETIME, SYSDATETIMEOFFSET()) AS NumberofSeconds;
Output
On executing the above query, it will generate the following output as shown below −
+--------------------+-----------------+ | MINUTE | NumberofMinutes | +--------------------+-----------------+ | Minutes_Difference | 391 | +--------------------+-----------------+ +--------------------+-----------------+ | SECOND | NumberofSeconds | +--------------------+-----------------+ | Seconds_Difference | 23460 | +--------------------+-----------------+
Example
In the following example, we are going to use DATENAME() to retrieve the month name from today´s date and time by running the following query −
SELECT DATENAME(month, SYSDATETIMEOFFSET()) AS PresentMonth;
Output
On running the above query, it will generate the output as shown below −
+--------------+ | PresentMonth | +--------------+ | February | +--------------+
Example
In the following example, we are going to use the DATEPART() function to retrieve the milliseconds based on the present date and time by running the following query −
SELECT DATEPART(millisecond, SYSDATETIMEOFFSET()) AS MilliSeconds;
Output
On executing the above query, it will generate the following output as shown below −
+--------------+ | MilliSeconds | +--------------+ | 718 | +--------------+
Example
Here, we are using the CONVERT() function and converting the return value into a time value by using the following query −
SELECT CONVERT (TIME, SYSDATETIMEOFFSET()) AS 'Time';
Output
On running the above query, it will generate the output as shown below −
+------------------+ | Time | +------------------+ | 18:00:20.2808586 | +------------------+
To Continue Learning Please Login
Login with Google