Convert seconds to a time values in MySQL; In this tutorial, i am going to show you how to convert second to time value (hh:mm:ss) using the MySQL sec_to_time() function with the help of examples.
MySQL SEC_TO_TIME() Function
The MySQL SEC_TO_TIME() function is used to return a time value in format HH:MM:SS from the specified number of seconds. In other words, it converts a value in seconds only to the format HH:MM:SS.
Syntax of MySQL SEC_TO_TIME() Function
The syntax of sec_to_time() function; as follows:
SEC_TO_TIME(seconds);
Hereseconds
is the number of seconds you want to be converted to a time value.
Example 1 – MySQL SEC_TO_TIME() Function
Let’s take a simple and basic example of this function; as follows:
SELECT SEC_TO_TIME(100);
Output-1
+------------------+ | SEC_TO_TIME(100) | +------------------+ | 00:01:40 | +------------------+
Example 2 – MySQL SEC_TO_TIME() Function
Let’s take an second example using MySQL sec_to_time() function with the large value; as follows:
SELECT SEC_TO_TIME(7555);
Output-2
+-------------------+ | SEC_TO_TIME(7555) | +-------------------+ | 02:05:55 | +-------------------+
Example 3 – MySQL SEC_TO_TIME() Function
Take an example of using the SEC_TO_TIME () function in a numerical context; as follows:
SELECT SEC_TO_TIME(4555), SEC_TO_TIME(4555) + 0;
Output-3
+-------------------+-----------------------+ | SEC_TO_TIME(4555) | SEC_TO_TIME(4555) + 0 | +-------------------+-----------------------+ | 01:15:55 | 115555| +-------------------+-----------------------+
Conclusion
MySQL SEC_TO_TIME() Function tutorial, you have learned how to use SEC_TO_TIME() function with various examples.