The TIME()
function in MySQL is used to extract the time portion from a date or datetime expression, returning the time in the format 'HH:MM'.
This function is particularly useful when working with time components in databases, such as scheduling or logging systems. In this article, We will learn about the TIME() function in MYSQL along with their syntax and so on.
TIME() Function in MySQL
- MySQL TIME() function is used to extract the time portion from a date or datetime expression.
- If the expression is not a time or a datetime value, the TIME function will return '00:00:00'.
- If an expression is NULL, the TIME function will return NULL.
- The TIME() function is particularly useful when you need to isolate and work with the time component of a datetime value.
Syntax
TIME(expression)
Parameter:
This method accepts one parameter.
- expression: The time or datetime value from which we want to extract the time.
Return Value:
- It returns the time in the format 'HH:MM:SS'.
Examples of TIME() function in MySQL
Let us take a look at some of the examples of TIME() function in MYSQL:
Example 1: Basic Usage of TIME() Function
Extracting the time using TIME Function from a datetime expression where the datetime is specified in the format of YYYY-MM-DD HH-MM-SS.
SELECT TIME("2019-01-10 08:14:21");
Output:
Example 2: Using TIME() Function in the format of HH-MM-SS
Extracting the time using TIME Function from a datetime expression where the datetime is specified in the format of HH-MM-SS.
SELECT TIME("18:24:23") AS Time;
Output:
Example 3:Using TIME() Function for expression which is not a datetime
In this example, we are extracting the time using TIME Function from a expression which is not a datetime.
SELECT TIME(NULL) AS TIME;
Output:
Example 4: Using TIME() function on table column example
The TIME function can be used to set value of columns. To demonstrate create a table named "DeliveryDetails".
CREATE TABLE DeliveryDetails (
DeliveryId INT AUTO_INCREMENT,
ProductId INT NOT NULL,
ProductName VARCHAR(20) NOT NULL,
Delivered_At TIME NOT NULL,
PRIMARY KEY(DeliveryId)
)
Here, we will use TIME function when a delivery will be completed. The value in Delivered_At column will be the value given by TIME Function.
INSERT INTO
DeliveryDetails(ProductId, ProductName, Delivered_At)
VALUES
(12345, 'Let Us Java', TIME (NOW()));
Now, checking the DeliveryDetails table :
SELECT * FROM DeliveryDetails;
Output:
DELIVERYID | PRODUCTID | PRODUCTNAME | DELIVERED_AT |
---|
1 | 12345 | Let Us Java | 09:48:34 |
Example 5: Using TIME() in WHERE Clause
You can also use the TIME() function in a WHERE clause to filter records based on the time part of a datetime value.
To demonstrate create a table named "events".
MySQL
CREATE TABLE events (
event_id INT PRIMARY KEY,
event_name VARCHAR(50),
event_datetime DATETIME
);
INSERT INTO events (event_id, event_name, event_datetime) VALUES
(1, 'Meeting', '2024-06-26 15:45:30'),
(2, 'Conference', '2024-06-26 10:30:00'),
(3, 'Workshop', '2024-06-26 15:45:30'),
(4, 'Webinar', '2024-06-26 12:00:00');
event_id | event_name | event_datetime |
---|
1 | Meeting | 2024-06-26 15:45:30 |
2 | Conference | 2024-06-26 10:30:00 |
3 | Workshop | 2024-06-26 15:45:30 |
4 | Webinar | 2024-06-26 12:00:00 |
When you run the query:
SELECT *
FROM events
WHERE TIME(event_datetime) = '15:45:30';
Output:
event_id | event_name | event_datetime |
---|
1 | Meeting | 2024-06-26 15:45:30 |
3 | Workshop | 2024-06-26 15:45:30 |
Important Points about TIME() Function in MySQL
Here are the important points about the TIME() function in MySQL:
- The
TIME()
function extracts the time part from a given date-time expression, returning the time in a hh:mm:ss[.nnnnnnn]
format. - It can handle precision up to seven decimal places for seconds, providing accurate time representation.
TIME() Function
can be used to convert various date-time formats to a time-only format, which is useful in scenarios where only the time component is required.- The function simplifies queries that involve time comparisons or calculations by isolating the time component from date-time values.
- It is commonly used in reporting, scheduling, and logging systems where operations based on time values are necessary.
Conclusion
The TIME()
function in MySQL is a valuable tool for extracting and working with the time component of datetime values. It allows developers to isolate the time from date-time expressions, making it particularly useful in time-specific operations such as filtering, logging, or reporting.
Similar Reads
SQL Interview Questions
Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970s, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial
SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands
SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join)
SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS
In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
8 min read
ACID Properties in DBMS
In the world of Database Management Systems (DBMS), transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliabilit
8 min read
Introduction of DBMS (Database Management System)
A Database Management System (DBMS) is a software solution designed to efficiently manage, organize, and retrieve data in a structured manner. It serves as a critical component in modern computing, enabling organizations to store, manipulate, and secure their data effectively. From small application
8 min read
SQL Query Interview Questions
SQL or Structured Query Language, is the standard language for managing and manipulating relational databases such as MySQL, Oracle, and PostgreSQL. It serves as a powerful tool for efficiently handling data whether retrieving specific data points, performing complex analysis, or modifying database
15 min read
CTE in SQL
In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Window Functions in SQL
SQL window functions are essential for advanced data analysis and database management. They enable calculations across a specific set of rows, known as a "window," while retaining the individual rows in the dataset. Unlike traditional aggregate functions that summarize data for the entire group, win
7 min read