Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

round up a decimal number

Posted on 2009-04-06
4
Medium Priority
?
1,483 Views
Last Modified: 2012-08-13
I have this line in my SP:
Cast(ceiling((TotalRows/@PageSize)) as int) as TotPages

I am using CTE and  ROW_NUMBER()OVER

when the TotalRows = 282 and @PageSize = 60 I am receiving TotPages = 4 while I really need 5 as the result.

How can this be done?
Thanks
0
Comment
Question by:ggilal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 24075166
Hi,

Try

Cast(ceiling((cast(TotalRows as decimal(6)/@PageSize)) as int) as TotPages

/peter
0
 
LVL 23

Accepted Solution

by:
adathelad earned 2000 total points
ID: 24075169
Hi,

You need to cast TotalRows and @PageSize to decimals to ensure degree of accuracy you require to then round up:

SELECT CAST(CEILING(CAST(TotalRows AS DECIMAL)/CAST(@PageSize AS DECIMAL)) AS INTEGER) as TotPages

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24075175
please try
Cast(ceiling( cast(TotalRows as decimal(10,2))/@PageSize) as int) as TotPages

Open in new window

0
 
LVL 4

Expert Comment

by:bleach77
ID: 24075293
In MSSQL, when you divide an integer with an integer, the result will be an integer. So in your case, 282/60 will be 4 instead of 4.7. So ceiling(4) will return 4. Just cast the variable with float or decimal or any like. it should work.
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

688 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