Home > Database > Mysql Tutorial > How to Round Minutes to Hours with Two Decimal Places in SQL?

How to Round Minutes to Hours with Two Decimal Places in SQL?

Linda Hamilton
Release: 2025-01-07 13:31:41
Original
728 people have browsed it

How to Round Minutes to Hours with Two Decimal Places in SQL?

Rounding Hours to Two Decimal Places with SQL

To convert minutes to hours and round the result to two decimal places, one approach is to use the ROUND function. However, the issue arises when the result contains more than two digits after the decimal point.

In the given example, dividing 630 minutes by 60 results in 10.5000000. To display only 10.50 after rounding, the result can be cast as a NUMERIC data type with a specified precision and scale.

Specifically, you would use CAST(ROUND(Minutes/60.0,2) AS NUMERIC(x,2)) where x is an integer less than or equal to 38. The following query demonstrates this approach:

SELECT
    ROUND(630/60.0,2), -- Output: 10.500000
    CAST(ROUND(630/60.0,2) AS NUMERIC(36,2)) -- Output: 10.50
Copy after login

The above is the detailed content of How to Round Minutes to Hours with Two Decimal Places in SQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template