MySQL - SEC_TO_TIME() Function



The MySQL SEC_TO_TIME() function accepts a numerical value representing seconds as an argument, converts it into TIME value (hours, minutes and seconds) and returns the result as a numerical value. If the seconds argument is either invalid or NULL, the return value is also NULL.

This function works oppositely to the TIME_TO_SEC() function. For instance, if the seconds value passed to this function is 1854 seconds, when converted to the timestamp, the result will be '00:30:54'.

Syntax

Following is the syntax of MySQL SEC_TO_TIME() function −

SEC_TO_TIME(time);

Parameters

This method accepts the number of seconds to be converted into a time value as a parameter.

Return value

This function returns a time value formatted as 'HH:MM:SS', where: 'HH' represents hours (00-23), 'MM' represents minutes (00-59), 'SS' represents seconds (00-59).

Example

Following example demonstrates the usage of the SEC_TO_TIME() function −

SELECT SEC_TO_TIME(71122) As Result;

Output

This will produce the following result −

Result
19:45:22

Following is another example of this function where we are converting the given seconds into a time format −

SELECT SEC_TO_TIME(28529) As Result;

Output

Following is the output −

Result
07:55:29

Example

In this example, we have created a table named SUBSCRIBERS using the following CREATE TABLE query −

CREATE TABLE SUBSCRIBERS (
   SUBSCRIBERNAME varchar(255),
   PACKAGENAME varchar(255),
   SUBSCRIPTIONTIMESTAMP int
);

Now, insert the following records into the SUBSCRIBERS table using the INSERT statement −

INSERT INTO SUBSCRIBERS VALUES
('Raja', 'Premium', 75229),
('Roja', 'Basic', 36799),
('Puja', 'Moderate', 20600),
('Vanaja', 'Basic', 59799),
('Jalaja', 'Premium', 45945);

Execute the below query to fetch all the inserted records in the above-created table −

Select * From SUBSCRIBERS;

Following is the SUBSCRIBERS table −

SUBSCRIBERNAMEPACKAGENAMESUBSCRIPTIONTIMESTAMP
RajaPremium75229
RojaBasic36799
PujaModerate20600
VanajaBasic59799
JalajaPremium45945

Here, we are using the MySQL SEC_TO_TIME() function to extract the time from the "SubscriptionTimestamp" column −

SELECT SubscriberName, SubscriptionTimeStamp, SEC_TO_TIME(SubscriptionTimestamp)
AS Time From SUBSCRIBERS;

Output

The output is displayed as follows −

SUBSCRIBERNAMESUBSCRIPTIONTIMESTAMPTime
Raja7522920:53:49
Roja3679910:13:19
Puja2060005:43:20
Vanaja5979916:36:39
Jalaja4594512:45:45