Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL Convert Time to Decimal Hours: A Clear Guide

· 5 min read · Home Tech

In this article
  1. Why Convert Time to Decimal Hours?
  2. How It Works in MySQL
  3. How It Works in SQL Server
  4. How It Works in PostgreSQL
  5. How It Works in SQLite
  6. Choosing the Right Method
  7. Common Mistakes and Pitfalls
  8. Alternatives to Decimal Hours
  9. Frequently Asked Questions

To convert a time value to decimal hours in SQL, multiply the time by 24 (if the time is stored as a fractional day) or extract hours, minutes, and seconds and divide by 3600. This guide explains the exact syntax for MySQL, SQL Server, PostgreSQL, and SQLite, with examples you can adapt.

Why Convert Time to Decimal Hours?

Time values like 08:30:00 are great for display, but they are awkward for arithmetic. If you need to calculate total hours worked, average task duration, or billable time, decimal hours (e.g., 8.5) are far easier to sum and multiply by a rate. Most SQL databases store time as a fraction of a 24-hour day, so converting is a matter of multiplying by 24. However, each database has its own quirks, which we cover below.

How It Works in MySQL

In MySQL, the TIME data type stores hours, minutes, and seconds. To convert to decimal hours, use TIME_TO_SEC() and divide by 3600:

SELECT TIME_TO_SEC('08:30:00') / 3600 AS decimal_hours;

Result: 8.5000. This works because TIME_TO_SEC() converts the time to total seconds, and dividing by 3600 gives hours. For a column named work_time, write:

SELECT work_time, TIME_TO_SEC(work_time) / 3600 AS decimal_hours FROM timesheet;

If your time exceeds 24 hours (e.g., 26:30:00), TIME_TO_SEC() still works correctly, returning 95400 seconds, which becomes 26.5 hours.

How It Works in SQL Server

SQL Server stores time as a time data type, but unlike MySQL, you cannot directly multiply it by 24. Instead, use DATEDIFF() with seconds and divide:

SELECT DATEDIFF(second, '00:00:00', '08:30:00') / 3600.0 AS decimal_hours;

Result: 8.500000. The division by 3600.0 (with a decimal point) is crucial—otherwise SQL Server performs integer division and returns 8. For a column, use:

SELECT DATEDIFF(second, '00:00:00', work_time) / 3600.0 AS decimal_hours FROM timesheet;

If your time values are stored as datetime or datetime2, you need to extract the time part first with CAST(work_time AS time).

How It Works in PostgreSQL

PostgreSQL treats time intervals and time-of-day differently. For a time column, you can extract seconds from midnight:

SELECT EXTRACT(EPOCH FROM '08:30:00'::time) / 3600 AS decimal_hours;

Result: 8.5000000000000000. For an interval column (e.g., a duration), use EXTRACT(EPOCH FROM interval) directly:

SELECT EXTRACT(EPOCH FROM INTERVAL '8 hours 30 minutes') / 3600;

This returns 8.5. The EPOCH function gives the total number of seconds, so dividing by 3600 yields decimal hours.

How It Works in SQLite

SQLite does not have a native time data type. Times are stored as text (e.g., '08:30:00') or as Julian day numbers. If stored as text, use strftime() to extract seconds:

SELECT (strftime('%s', '1970-01-01 ' || '08:30:00') - strftime('%s', '1970-01-01')) / 3600.0 AS decimal_hours;

Result: 8.5. A simpler approach is to parse the text manually:

SELECT CAST(substr('08:30:00', 1, 2) AS INTEGER) + CAST(substr('08:30:00', 4, 2) AS INTEGER) / 60.0 + CAST(substr('08:30:00', 7, 2) AS INTEGER) / 3600.0;

This avoids the date arithmetic and works for any time string in HH:MM:SS format.

Choosing the Right Method

Your choice depends on your database and data type:

  • MySQL: Use TIME_TO_SEC() for TIME columns. It handles values over 24 hours.
  • SQL Server: Use DATEDIFF(second, '00:00:00', time) and divide by 3600.0. Be careful with integer division.
  • PostgreSQL: Use EXTRACT(EPOCH FROM ...) for both time and interval types.
  • SQLite: Parse the text or use strftime with a base date.

If your time values are stored as strings in other formats (e.g., '8:30 AM'), you will need to convert them to a standard 24-hour format first, which is a separate challenge.

Common Mistakes and Pitfalls

  • Integer division: In SQL Server and SQLite, dividing an integer by an integer yields an integer. Always divide by 3600.0 or cast to a decimal.
  • Ignoring seconds: If you only extract hours and minutes, you lose seconds. For precise billing, include seconds in the conversion.
  • Times over 24 hours: In MySQL, TIME supports values like 26:30:00, but TIME_TO_SEC() handles them. In SQL Server, the time type is limited to 24 hours, so you must use datetime or an interval type.
  • Rounding: Decide how many decimal places you need. For payroll, 2 or 4 decimal places are common. Use ROUND() if necessary.

Alternatives to Decimal Hours

Sometimes decimal hours are not the best choice. Consider these alternatives:

  • Minutes: Store total minutes as an integer. This avoids floating-point errors and is easy to convert to hours for display.
  • Seconds: For high precision, store total seconds. This is the most accurate and avoids rounding issues.
  • Interval types: PostgreSQL and SQL Server have native interval/duration types that support arithmetic directly. You can add two intervals without converting to decimal.

If you only need to sum durations, using SECOND or MINUTE might be simpler than converting to decimal hours.

Frequently Asked Questions

How do I convert a time with AM/PM to decimal hours?

First, convert the 12-hour format to 24-hour format. In MySQL, you can use STR_TO_DATE('8:30 PM', '%h:%i %p') to get a time, then apply TIME_TO_SEC(). In SQL Server, use CONVERT(time, '8:30 PM', 0) (depending on locale). In PostgreSQL, cast the string to time after converting to 24-hour format.

What if my time is stored as a datetime column?

Extract the time part first. In MySQL, use TIME(column). In SQL Server, use CAST(column AS time). In PostgreSQL, use column::time. Then apply the conversion method for that database.

How do I convert decimal hours back to time?

Multiply by 3600 to get seconds, then convert to a time. In MySQL, use SEC_TO_TIME(decimal_hours * 3600). In SQL Server, use DATEADD(second, decimal_hours * 3600, '00:00:00'). In PostgreSQL, use make_interval(secs => decimal_hours * 3600).

Why is my result showing 8 instead of 8.5?

This is almost always integer division. In SQL Server, dividing DATEDIFF(second, ...) by 3600 (an integer) returns an integer. Use 3600.0 or CAST(3600 AS decimal). The same issue occurs in SQLite when dividing by 3600 instead of 3600.0.

Can I convert a time interval that exceeds 24 hours?

Yes, but only if your data type supports it. MySQL's TIME can store up to 838 hours. PostgreSQL's interval has no practical limit. SQL Server's time type is capped at 24 hours, so you must use datetime or a custom solution.

Does the conversion handle negative times?

Some databases allow negative time values (e.g., to represent a deficit). In MySQL, TIME_TO_SEC() returns a negative number for negative times, and dividing by 3600 gives a negative decimal. In SQL Server, DATEDIFF with a negative time may behave unexpectedly; test with your data.