简体   繁体   中英

SQL rounding time to nearest 30 minutes

I am using this SQL Query:

SELECT ROUND((hours*60+minutes)/30) from table 

which calculates the number 30 minutes segments

if i have 2 hours, 10 minutes this is returning 30_minute_segments = '4' however i would like it to show 5 as the 10 minutes should count as 30 minutes.

how can i do this?

I think you want to use CEIL instead of ROUND . CEIL will round up.

SELECT CEIL((hours*60+minutes)/30) from table

I just realized that I assumed you were using Oracle. But you didn't specify your database.

For SQL Server, the equivalent function is CEILING() . Most databases have something similar. (Note: if using SQL Server, be mindful of @Chizzle's comment below.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM