- 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 - DATETIMEOFFSETFROMPARTS() Function
The SQL DATETIMEOFFSETFROMPARTS() function enables you to extract a datetimeoffset value from each of a date's component parts. It specifically returns a datetimeoffset value for the given time and date, along with the offsets and precision.
DATETIMEOFFSETFROMPARTS is one of the crucial SQL server operations used when making date and time from offset and precision. Based on the given date and time component inputs, a value is returned. Every argument is necessary. Null is returned if any is null.
Syntax
Following is the syntax for SQL DATETIMEOFFSETFROMPARTS() function −
DATETIMEOFFSETFROMPARTS ( year, month, day, hour, minute, seconds, fractions, hour_offset, minute_offset, precision )
Parameters
This function accepts ten parameter as discussed below −
year − integer expression that indicates year.
month − integer expression that indicates month.
day − integer expression that indicates day.
hour − integer expression that indicates hours.
minute − integer expression that indicates minutes.
seconds − integer expression that indicates seconds.
fractions − integer expression that indicates a fractional seconds value.
hour_offset − integer expression that indicates the hour portion of time zone offset.
minute_offset − integer expression that indicates the minute portion of time zone offset.
precision − integer value that indicates the precision of the datetimeoffset value that DATETIMEOFFSETFROMPARTS will return.
Example
Here, we are using theDATETIMEOFFSETFROMPARTS() function along with date and time arguments to construct DATETIMEOFFSET by using the following query −
SELECT DATETIMEOFFSETFROMPARTS(2023, 02, 21, 12, 28, 30, 5000, 10, 30, 4) AS Result;
Output
When we execute the above query, the output is obtained as follows −
+---------------------------------+ | Result | +---------------------------------+ | 2023-02-21 12:28:30.5000 +10:30 | +---------------------------------+
Example
Let us look into in the another scenario, where we are going to use the null value for the date and checking the result by running the following query −
SELECT DATETIMEOFFSETFROMPARTS(2023, 02, NULL, 12, 28, 30, 5000, 10, 30, 4) AS Result;
Output
When the query, gets executed it will generate the output as shown below −
+-------+ | Result| +-------+ | NULL | +-------+
Example
In the following example, we are going to use the invalid arguments which causes the DATETIME2FROMPARTS() function returns an error, let's check this by running the following query −
SELECT DATETIMEOFFSETFROMPARTS(2023, 22, 21, 12, 28, 30, 5000, 10, 30, 4) AS Result;
Output
On executing the above query, the output is displayed as follows−
Cannot construct data type datetimeoffset, some of the arguments have values which are not valid.
Example
Let us look into the another scenario, where we are not going to specify the total number of arguments and checking the result by running the query −
SELECT DATETIMEOFFSETFROMPARTS(2023, 22, 21) AS Result;
Output
When the query, gets executed it will generate the output as shown below −
The datetimeoffsetfromparts function requires 10 argument(s).
Example
Here, we are going to mention the last precision value to be NULL and checking the result by running the following query −
SELECT DATETIMEOFFSETFROMPARTS(2023, 22, 21, 12, 28, 30, 5000, 10, 30, NULL) AS Result;
Output
The output for the above query is produced as given below −
Scale argument is not valid. Valid expressions for data type datetimeoffset scale argument are integer constants and integer constant expressions.
Example
In the following example we are going to mention the precision value greater than fractional and checking the result by using the following query −
SELECT DATETIMEOFFSETFROMPARTS(2023, 02, 21, 12, 28, 30, 363, 10, 30, 7) AS Result;
Output
When the query gets executed, it will generate an output as shown below −
+------------------------------------+ | Result | +------------------------------------+ | 2023-02-21 12:28:30.0000363 +10:30 | +------------------------------------+
Example
Let us consider another scenario, where we are going to use precision value less than fractional and checking the result by using the following query −
SELECT DATETIMEOFFSETFROMPARTS(2023, 02, 21, 12, 28, 30, 363, 10, 30, 2) AS Result;
Output
On running the above query, it will generate an output as shown below −
Cannot construct data type datetimeoffset, some of the arguments have values which are not valid.
To Continue Learning Please Login
Login with Google