Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Rounding Up

Posted on 2014-10-30
Medium Priority
99 Views
Hi

SQL 2008

I am trying to round up a division I am doing so  as follows

For example I have data for a duration of 740 secs, I want to convert this into minutes (740/60).
The answer would be 12.33 (displayed in SQL as 12). I want to round this up to the next whole number which would be 13. IF it was 720 secs that would stay at 12

How can this be done

Thanks
0
Question by:halifaxman

LVL 66

Accepted Solution

Jim Horn earned 2000 total points
ID: 40413343
What's the logic for rounding up vs. down?  Does 12.01 round up to 13 or stay at 12?
So far..
``````Declare @numerator int = 740, @denominator int = 60

SELECT CAST(@numerator as decimal(9,2)) / @denominator as decimal_value

-- If 12.01 rounds up to 13
SELECT CASE
WHEN @numerator / @denominator = CAST(@numerator as decimal(9,2)) / @denominator THEN @numerator / @denominator
ELSE @numerator / @denominator + 1 END as integer_value
``````
0

LVL 36

Expert Comment

ID: 40413393
12,33 are 12 hours and 20 minutes. Why rounding to 13?
0

LVL 32

Expert Comment

ID: 40413924
The ceiling function should do what you want.
ceiling(740/60) => 13
0

LVL 49

Expert Comment

ID: 40415191
is 1 second = to 1 minute?

``````select
secs
, case when secs % 60 = 0 then secs / 60 else (secs / 60) + 1 end
from table1
``````

``````| SECS | COLUMN_1 |
|------|----------|
|    1 |        1 |
|    2 |        1 |
|    3 |        1 |
..
|   59 |        1 |
|   60 |        1 |
|   61 |        2 |
...
|  739 |       13 |
|  740 |       13 |
|  741 |       13 |
--
``````
see http://sqlfiddle.com/#!3/19a0a/2
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
###### Suggested Courses
Course of the Month14 days, 22 hours left to enroll