Solved

Rounding Up

Posted on 2014-10-30
4
90 Views
Last Modified: 2014-11-24
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
Comment
Question by:halifaxman
4 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 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

Open in new window

0
 
LVL 33

Expert Comment

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

Expert Comment

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

Expert Comment

by:PortletPaul
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

Open in new window


| SECS | COLUMN_1 |
|------|----------|
|    1 |        1 |
|    2 |        1 |
|    3 |        1 |
..
|   59 |        1 |
|   60 |        1 |
|   61 |        2 |
...
|  739 |       13 |
|  740 |       13 |
|  741 |       13 |
--

Open in new window

see http://sqlfiddle.com/#!3/19a0a/2
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Pivot Rows To Columns 10 51
Addition to SQL for dynamic fields 6 36
sql server concatenate fields 10 31
SQL Error - Query 6 24
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now