11

How would you convert secs to HH:MM:SS format in SQLite?

3 Answers 3

20

Try this one:

sqlite> SELECT time(10, 'unixepoch');
00:00:10
3
  • This is what I thought originally but used strftime instead of time
    – vfclists
    Apr 21, 2010 at 22:24
  • What about the reverse? That is, from 00:00:10 to 10. Aug 19, 2018 at 22:21
  • 1
    This is limited to time 24 hours or less. Beyond 24 hours, the resulting value is the time of day from the following day(s). i.e. select time(60*60*24+600,'unixepoch') does not yield 24:10; it yields 00:10 (the next day's 10 minutes). The correct answer is by mivk. Formatted time would be printf('%3d', Seconds / 3600)) || ':' || strftime('%M:%S', Seconds / 86400.0) Mar 15, 2020 at 20:12
12

If your seconds are more than 24 hours, you need to calculate it yourself. For example, with 100123 seconds, rounding down to minutes:

SELECT (100123/3600) || ' hours, ' || (100123%3600/60) ||' minutes.'
27 hours, 48 minutes

The time or strftime functions will obviously convert every 24-hours to another day. So the following shows 3 hours (the time on the next day):

SELECT time(100123, 'unixepoch')
03:48:43

To get the full 27 hours, you can calculate the hours separately, and then use strftime for the minutes and seconds:

SELECT (100123/3600) || ':' || strftime('%M:%S', 100123/86400.0);
27:48:43
0

Well, i would do something like this, but i'm getting hours plus 12...

SELECT strftime('%H-%M-%S', CAST(<seconds here> / 86400.0 AS DATETIME)) 

-- For subtracting the 12 hours difference :S
SELECT strftime('%H-%M-%S', CAST(<seconds here> / 86400.0 AS DATETIME) - 0.5) 
2
  • I think it is adjustjing for the start of the epoch. I know it can work, I will work it out.
    – vfclists
    Apr 21, 2010 at 19:57
  • well, thanks for the nudge !! I thought nobody was going to answer so I dropped it out Apr 21, 2010 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.