Changing Seconds to a Time in MySQL

Last updated 2 years ago

...

The visitors table in our database comprises of id, country_code, date, and time columns filled with relevant data.

visitors table

idcountry_codedatetime
1AE2023-05-101124
2AU2023-05-10987
3CA2023-05-101003
4SA2023-05-101233

 

Obtain the country code along with the timed record of every visitors from the time column. The current format expresses it as a number of seconds; hence, let's showcase it as HH:MM:SS.

 

Let's see the solution

We shall utilize SEC_TO_TIME() function and here's the corresponding query you need to compose.

SELECT country_code,SEC_TO_TIME(time) AS visited_time FROM visitors;

Output:

country_codevisited_time
AE00:18:44
AU00:16:27
CA00:16:43
SA00:20:33


If you aim to showcase a certain number of seconds as a time value in MySQL, the SEC_TO_TIME() function is what you should employ. This function necessitates only one argument to run, specifically an expression that renders seconds as an integer or a column that stores seconds in an integer format. (In the illustrated case, we utilize the column time.)

The function SEC_TO_TIME() produces time in HH:MM:SS form, with hours represented from 00 to 59, minutes from 01 to 59, and seconds from 00 to 59.

 


MySql: Calculate the Diff...

You possess a pair of columns labeled as "timestamp" and wish to determine the duration between thei...

Read It

An Introduction To CSS Pr...

CSS or Cascading Style Sheets have become a really popular programming language in recent times and...

Read It



----Advertisement----