Round Up Float column to next number with a multiple of 5 - sql

Currently I have a column defined as a float(ex 16.98) I need to be able to take the value and find the next highest multiple of 5.
Examples...
Value 16.98 Should Return 17.00
Value 10.46 Should Return 10.50
Value 9.11 Should Return 9.15
Value 8.10 Should Return 8.10
Value 18.65 Should Return 18.65
Notice that if is is a multiple of 5 then it should return itself.

If You are using MySql try this:
SELECT (ceil(cast(val*100 as signed)/5.0)*5.0)/100.0 from tbl;
CAST inside CEIL is very helpful - it eliminates rounding error.
Here is Demo
In SQL SERVER:
SELECT (ceiling(cast(val*100 as int)/5.0)*5.0)/100.0 from tbl;
Where tbl contains values to transform.

SELECT Format((ceiling(cast(unitprice*100 as int)/5.0)*5.0)/100.0, 'g18')
From table

Try this:
SELECT
CEILING(16.98 * 20) / 20,
CEILING(10.46 * 20) / 20,
CEILING(9.11 * 20) / 20,
CEILING(8.10 * 20) / 20,
CEILING(18.65 * 20) / 20
So the query would look like:
SELECT CEILING(yourColumn * 20) / 20 AS Result FROM yourTable;

Related

Sum column only 2 digits after precision (no round)

I have a float type column with numbers that have 6 digits after precision. I want to sum the column only by 2 digits after precision.
For example, I have 1.257868 and 1.258778 as values and I want to get the result of 2.50 as result of sum.
It looks like you want something like:
sum(floor(mycol * 100) / 100)
The expression within the sum() performs the truncation to 2 decimals.
You can use the 3 argument form of round():
select sum(round(mycol, 2, 1))
odbc truncate
select v.val, {fn TRUNCATE(v.val, 2)} as trncted, sum({fn TRUNCATE(v.val, 2)}) over() as sumtrncated
from
(values (1.257868), (1.258778)) as v(val);

Inconsistent results when using Round in Oracle

I have an Oracle function. Due to privacy issues, I can't include the whole function, but the relevant line is :-
WHEN in_tariff_length = 36 THEN round( ( ( (in_agreed / 100) * 80) * in_uplift) / 100,2) * 3
..which is rounded to 2 decimal places, and returns 1655.58
When I use
WHEN in_tariff_length = 36 THEN round( ( ( (in_agreed / 100) * 80) * in_uplift) / 100,3) * 3
..which is rounded to 3 decimal places, and returns 1655.568
The result I need is 1655.57.
All values sent to and returned from the function are NUMBER.
You can't round something, multiply by 3 and get 1655.57
551.86 * 3 = 1655.58
551.856 * 3 = 1655.568
1655.57 / 3 = 551.856666667
I suggest multiply first, then round.
Please look closely at the second function
round( ( ( (in_agreed / 100) * 80) * in_uplift) / 100,3)
Round( something, 3) means round to 3 decimal places but not to 2

CEILING in SQL does not return correct values

CEILING in T-SQL does not return correct values in SQL Server 2012. Please see below details.
select CEILING(0.3333333333333333)
Result: 1
But I want to do a calculation of based on count of columns. Internal calculation is given below.
select CEILING((17 - 2) / 45)
Result: 0
(17 - 2) / 45 = 0.33333
But
select CEILING((17 - 2) / 45)
returns zero
I want to get one as result. What should I do?
It ended with a integer division. So
15/45 = 0
Try CASTing atleast one of the operand into decimal ,
SELECT CEILING((17-2)/CAST(45 AS DECIMAL (4,2))
OR like,
SELECT CEILING(15,45.0)

Round Function in SQl

select Round((10*100)/115,0)
Result : 8
But Result : 8.69
But i want to display 9
I tried below query also, but result is same..
select Round((10*100)/115,0)
Please solve my prob....
Thanks
Try below
declare #n decimal(4,1)
select #n = 8.69
select case when PARSENAME(#n,1)>=5 then ceiling(#n) else floor(#n) end
idea is if the number after point is greater than 5 then go to upper value else go to lower one
If your database is oracle then the following function you can use..
For result 9 use ceil function and For result 8 use floor function
select ceil(8.69) val from dual;
select floor(8.69) val from dual;
for upper value you can use Ceil function and for lower value you can use floor function in oracle as below
select ceil(8.69) Upper_val,floor(8.69) Lower_val from dual;
It returns an integer. Try to convert it to a decimal before calculation
Eg.
select Round((10*100)/115,0)
change to
select Round((10.0*100)/115,0)
Hope this will help
Here is what's happening:
Even before using ROUND Function
SELECT 1000/115 /* the result is 8 */
and try this
SELECT ROUND(1000.0/15, 0) /* the result is 9.000000 */
and if use this
SELECT CEILING(1000.0/115) /* the result is 9 */
so basically before call any function the result is 8.
you can use this:
SELECT ROUND(CONVERT(decimal, 1000) / 115, 0)
or simply this:
SELECT ROUND(1000.0/15, 0)
If Its SQL Server please do try this:
SELECT CEILING(10.1) AS UpperValue, FLOOR(10.6) AS LowerValue,
Round(CAST((CAST(10 AS float) * CAST(100 AS float)) / CAST(115 AS float) AS float), 0)
AS YourValue, Round((10*100)/115,0) AS YourOldValue;

Removing zero from decmial

I got a output like this
0.00234690616839645663803848176618444236941
the way I want is
2.3
first I remove the zero
select replace(0.00234690616839645663803848176618444236941,0) from dual;
then I try to do the round function on it , but its giving me zero any idea how can we get this
select round(replace(0.00234690616839645663803848176618444236941,0) ) from dual;
You can try this :
select round(0.00234690616839645663803848176618444236941 * 1000,1) from dual;
Result:
2.3
When you strip out the zeros, you are left with .23.....
Select round (.23, 0) will return 0, because you are telling the db to round to 0 decimal places.
If you multiply the result of your replace by 10, that will get you want you want. Not sure what you are doing makes any sense, but it works:
select round (10 * (replace(0.00234690616839645663803848176618444236941,0)),1) from dual;
SQL Fiddle