How the CURRENT_TIME() function works in Mariadb?
The CURRENT_TIME() function is a date and time function that returns the current time as a value in HH:MM:SS format.
The MariaDB CURRENT_TIME() function is used to obtain the current time in HH:MM:SS format, which is essential for recording time-specific data and performing time-based calculations within database operations.
Syntax
The syntax for the MariaDB CURRENT_TIME() function is as follows:
CURRENT_TIME()
This function does not require any parameters and returns the current time as a string in HH:MM:SS format.
Examples
Retrieve the Current Time
To get the current time, you can use the CURRENT_TIME() function in the following way:
SELECT CURRENT_TIME();
The output will display the current time:
+----------------+
| CURRENT_TIME() |
+----------------+
| 09:23:29 |
+----------------+Time Comparison
If you need to compare the current time with a specific time to check if they are the same, you can use the CURRENT_TIME() function:
SELECT CURRENT_TIME() = '10:29:15' AS Is_Current_Time;
The output will show whether the current time matches the specified time:
+-----------------+
| Is_Current_Time |
+-----------------+
| 0 |
+-----------------+Extract Hour from Current Time
To extract the hour part from the current time, use the CURRENT_TIME() function like this:
SELECT HOUR(CURRENT_TIME()) AS Current_Hour;
This will return the hour part of the current time:
+--------------+
| Current_Hour |
+--------------+
| 9 |
+--------------+Check if It’s Past Noon
You can check if the current time is past noon using the CURRENT_TIME() function:
SELECT CURRENT_TIME() > '12:00:00' AS Is_Afternoon;
The output will indicate whether it’s past noon:
+--------------+
| Is_Afternoon |
+--------------+
| 0 |
+--------------+Use CURRENT_TIME() in Conditional Statements
The CURRENT_TIME() function can be used in conditional statements within your queries:
SELECT CASE
WHEN CURRENT_TIME() < '12:00:00' THEN 'Good morning'
WHEN CURRENT_TIME() < '18:00:00' THEN 'Good afternoon'
ELSE 'Good evening'
END AS Greeting;
The output will provide an appropriate greeting based on the current time:
+--------------+
| Greeting |
+--------------+
| Good morning |
+--------------+Related Functions
Here are a few functions related to the MariaDB CURRENT_TIME() function:
- MariaDB
CURTIME()function is used as a synonym forCURRENT_TIME(). - MariaDB
NOW()function returns the current date and time. - MariaDB
SYSDATE()function returns the current date and time, but unlikeNOW(), it returns the exact time the function is executed.
Conclusion
The CURRENT_TIME() function in MariaDB is a straightforward and powerful tool for managing time-related data within your databases. It provides precision and convenience for time-based queries and is an essential function for any database administrator or developer working with time-sensitive information. Understanding how to use CURRENT_TIME() effectively can greatly enhance the functionality of your database applications.