Sai A Sai A
Updated date Aug 02, 2023
In this blog, we will provide a complete guide on converting seconds to time format in MySQL. It presents multiple methods to achieve the conversion, including using built-in functions like TIME_FORMAT() and DATE_FORMAT(), as well as creating a custom function.

Introduction:

In MySQL, handling time-related data is a common task for database administrators and developers. While MySQL provides excellent support for storing and manipulating time values, it might sometimes be necessary to convert seconds into a more readable time format. This blog aims to provide a step-by-step guide on converting seconds to time format in MySQL.

Method 1: Using TIME_FORMAT() Function

MySQL provides the TIME_FORMAT() function, which allows us to format time values easily. To convert seconds to time format using this method, we need to divide the given number of seconds by 3600 to get the hours, by 60 to get the minutes, and then use the TIME_FORMAT() function to combine them into a time format.

Here's the program in SQL:

SELECT TIME_FORMAT(SEC_TO_TIME(seconds), '%H:%i:%s') AS time_format FROM your_table;
  • The SEC_TO_TIME(seconds) function converts the given number of seconds to a TIME data type.
  • The %H in the TIME_FORMAT() function represents hours, %i represents minutes, and %s represents seconds.

Output:

If you have a table named your_table with a column named seconds containing the number of seconds, the output of this query will be a time format string for each record in the table.

Method 2: Using DATE_FORMAT() Function

Another approach to convert seconds to time format is by using the DATE_FORMAT() function. This method involves converting the seconds to a valid date and then formatting the date as required.

Here's the SQL program:

SELECT DATE_FORMAT(FROM_UNIXTIME(seconds), '%H:%i:%s') AS time_format FROM your_table;
  • The FROM_UNIXTIME(seconds) function converts the given number of seconds to a DATETIME data type.
  • The DATE_FORMAT() function then formats the DATETIME as a time format using the specified format string.

Output:

Similar to Method 1, the output will be a time format string for each record in the table.

Method 3: Using Custom Function

If you find yourself needing to perform this conversion frequently or want more flexibility, you can create a custom function to achieve the task. Here's a sample custom function:

DELIMITER //
CREATE FUNCTION ConvertSecondsToTime(seconds INT)
RETURNS VARCHAR(8)
BEGIN
    DECLARE time_string VARCHAR(8);
    SET time_string = CONCAT(
        LPAD(FLOOR(seconds / 3600), 2, '0'), ':',
        LPAD(FLOOR(seconds % 3600 / 60), 2, '0'), ':',
        LPAD(seconds % 60, 2, '0')
    );
    RETURN time_string;
END;
//
DELIMITER ;
  • The ConvertSecondsToTime function takes the number of seconds as input and returns a formatted time string.
  • Inside the function, the LPAD() function is used to pad single-digit hours, minutes, and seconds with leading zeros if necessary.

Output:

You can use this custom function in your SQL queries as follows:

SELECT ConvertSecondsToTime(seconds) AS time_format FROM your_table;

The output will be a time format string for each record in the table.

Conclusion:

In this blog, we explored various methods to convert seconds to time format in MySQL. We started by using the TIME_FORMAT() function, which is a direct and straightforward approach. Then, we discussed using the DATE_FORMAT() function to format the time after converting seconds to a valid date. Finally, we explored creating a custom function, which provides greater flexibility and reusability for the conversion process.

Comments (0)

There are no comments. Be the first to comment!!!