- 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 - SMALLDATETIMEFROMPARTS() Function
The SQL SMALLDATETIMEFROMPARTS() function is used to construct a new datetime value from individual segments provided as parameters to this function.
This function accepts five parameters − year, month, day, hour, and minute and returns a datetime value; it returns an error if any of the parameters are missing.
It returns an error if any of the parameters are missing.
If the values passed as parameters are invalid, this function will result in an error.
If any of the parameters are null, it will return the result as null.
Syntax
Following is the syntax of the SQL SMALLDATETIMEFROMPARTS() function −
SMALLDATETIMEFROMPARTS(year, month, day, hour, minute)
Parameters
This function accepts five parameters. The same is described below −
year − This specifies the year segment of the datetime2 value, expressed as an integer.
month − This specifies the month segment of the datetime2 value, expressed as an integer from 1 to 12.
day − This specifies the day segment of the datetime2 value, expressed as an integer from 1 to 31.
hour − This specifies the hour segment of the datetime2 value, expressed as an integer from 0 to 23.
minute − This specifies the minute segment of the datetime2 value, expressed as an integer from 0 to 59.
Example
The following example demonstrates the usage of the SQL SMALLDATETIMEFROMPARTS() function −
SQL> SELECT SMALLDATETIMEFROMPARTS(2023, 02, 18, 16, 30) AS RESULT;
Output
On executing the above query, the output is displayed as follows −
+---------------------+ | RESULT | +---------------------+ | 2023-02-18 16:30:00 | +---------------------+
Example
If we provide invalid values to any of the parameters of the function, it results an error.
SQL> SELECT SMALLDATETIMEFROMPARTS(2023, 02, 34, 87, 72) AS RESULT;
Error
If we execute the program, the result is produced as follows −
Cannot construct data type datetime, some of the arguments have values which are not valid.
Example
If we provide any of the parameters of the function as null, the function will result as null.
SQL> SELECT SMALLDATETIMEFROMPARTS (2023, 02, null, 23, null) AS RESULT;
Output
When we execute the above query, the output is obtained as follows −
+-----------+ | DATE_TIME | +-----------+ | NULL | +-----------+
Example
We need to provide all FIVE parameters of the function. If we do not provide it, the function will result in an error.
SQL> SELECT SMALLDATETIMEFROMPARTS(2023, 02, 18) AS DATE_TIME;
Error
If we execute the above query, the result is produced as follows −
The smalldatetimefromparts function requires 5 argument(s)..
Example
Here, we are providing the leap year date as parameters to the function using the following query −
SQL> SELECT SMALLDATETIMEFROMPARTS (2024, 02, 29, 19, 11) AS RESULT
Output
When we execute the above query, the output is obtained as follows −
+---------------------+ | RESULT | +---------------------+ | 2024-02-29 19:11:00 | +---------------------+
Example
In the following example, we are providing 29 as the date and the year 2023 is not a leap year. So, the function will return an error.
SQL> SELECT SMALLDATETIMEFROMPARTS (2023, 02, 29, 19, 11) AS RESULT
Error
If we execute the program, the result is produced as follows −
Cannot construct data type datetime, some of the arguments have values which are not valid.
Example
Assume we have created a table with the name STUDENTS in the SQL database using the CREATE statement as shown in the query below −
SQL> CREATE TABLE STUDENTS(ID INT NOT NULL, NAME VARCHAR (200) NOT NULL, YEAR VARCHAR (200) NOT NULL, MONTH VARCHAR (200) NOT NULL, DAY VARCHAR (200) NOT NULL, HOURS VARCHAR (200) NOT NULL, MINUTES VARCHAR (200) NOT NULL)
Now, let us insert some records in the STUDENTS table using INSERT statements as shown in the query below −
SQL> INSERT INTO STUDENTS(ID, NAME, YEAR, MONTH, DAY, HOURS, MINUTES) VALUES(1, 'Dhruv', '2023', '01', '01', '14', '23'); INSERT INTO STUDENTS(ID, NAME, YEAR, MONTH, DAY, HOURS, MINUTES) VALUES(2, 'Arjun', '2023', '02', '01', '05', '43'); INSERT INTO STUDENTS(ID, NAME, YEAR, MONTH, DAY, HOURS, MINUTES) VALUES(3, 'Dev', '2023', '03', '01', '06', '11'); INSERT INTO STUDENTS(ID, NAME, YEAR, MONTH, DAY, HOURS, MINUTES) VALUES(4, 'Riya', '2023', '04', '01', '07', '21'); INSERT INTO STUDENTS(ID, NAME, YEAR, MONTH, DAY, HOURS, MINUTES) VALUES(5, 'Aarohi', '2023', '08', '01', '08', '54'); INSERT INTO STUDENTS(ID, NAME, YEAR, MONTH, DAY, HOURS, MINUTES) VALUES(6, 'Lisa', '2023', '09', '01', '09', '32'); INSERT INTO STUDENTS(ID, NAME, YEAR, MONTH, DAY, HOURS, MINUTES) VALUES(7, 'Roy', '2023', '10', '01', '10', '01');
We can verify whether the table is created or not using the following query −
SQL> SELECT * FROM STUDENTS
The table STUDENTS is successfully created in the SQL database.
+----+--------+------+-------+-----+-------+---------+ | ID | NAME | YEAR | MONTH | DAY | HOURS | MINUTES | +----+--------+------+-------+-----+-------+---------+ | 1 | Dhruv | 2023 | 01 | 01 | 14 | 23 | | 2 | Arjun | 2023 | 02 | 01 | 05 | 43 | | 3 | Dev | 2023 | 03 | 01 | 06 | 11 | | 4 | Riya | 2023 | 04 | 01 | 07 | 21 | | 5 | Aarohi | 2023 | 08 | 01 | 08 | 54 | | 6 | Lisa | 2023 | 09 | 01 | 09 | 32 | | 7 | Roy | 2023 | 10 | 01 | 10 | 01 | +----+--------+------+-------+-----+-------+---------+
Here, we are trying to join all the date and time values of the students using the following query −
SQL> SELECT NAME, SMALLDATETIMEFROMPARTS(YEAR, MONTH, DAY, HOURS, MINUTES) AS SMALLDATETIMEFROMPARTS_VALUE FROM STUDENTS;
Output
When we execute the above query, the output is obtained as follows −
+--------+-------------------------+ | NAME | DATETIMEFROMPARTS_VALUE | +--------+-------------------------+ | Dhruv | 2023-01-01 14:23:00 | | Arjun | 2023-02-01 05:43:00 | | Dev | 2023-03-01 06:11:00 | | Riya | 2023-04-01 07:21:00 | | Aarohi | 2023-08-01 08:54:00 | | Lisa | 2023-09-01 09:32:00 | | Roy | 2023-10-01 10:01:00 | +--------+-------------------------+
To Continue Learning Please Login
Login with Google