How to round off numbers in SQL Server database - sql

I have a column (named PercentageDifference) with numbers that have decimal places as shown below:
PercentageDifference
1.886792452830100
2.325581395348800
2.758620689655100
-3.689320388349500
-0.284900284900200
0.400000000000000
I want a query to round off the numbers to the nearest 10 and then leave 2 decimal places.
Here is the output am looking for:
PercentageDifference
1.89
2.33
2.76
-3.69
-0.28
0.40
I have tried to use the ROUND function but its not giving me the expected results:
select round([Percentage Difference], 2, 1) from Table
How can this be achieved?

You need only CAST:
SELECT CAST([Percentage Difference] AS decimal(19,2)) FROM Table;

Related

How to select only two decimal number without rounding in sql?

My problem is I want to select two decimal number in amount column without rounding up. I have tried use FORMAT but rounding up my values
sql:
SELECT claim_type,amount
FROM detail
table detail :
id claim_type amount
------ -------------------- -------
1 FOOD REFRESHMENT 1.27000
2 FOOD REFRESHMENT 2.35000
I want the output be like this when I select
claim_type amount
-------------------- -------
FOOD REFRESHMENT 1.27
FOOD REFRESHMENT 2.35
This is dependent on the dialect of SQL, but in general your examples won't be rounded by either FORMAT or TRUNCATE analogues. If you have a number such as 2.237 and try to format it to two numbers after the decimal point, it will be rounded up to 2.24 by those functions - but you can always cast it to a string datatype and then simply take a substring. In PostgreSQL:
% select substring(2.237::text, 1, 4);
substring
-----------
2.23
(1 row)
you can also use the Cast function to get decimals values without rounding as below
SELECT Cast(Round(123.419,2,1) as decimal(18,2))

Can a format value "divide" a number in PostgreSQL?

One of the columns of my table has values that can typically range from 3500 to 8 million. Is it possible to specify a format that can divide the number when formatting?
For example, I have the following values:
3500
81000
1678500
Ideally I would like a format value (coming from another config table) that would format the numbers in "thousands" with 1 decimal place:
3.5
81.0
1678.5
But this format value could also be different for other cases, so they could be formatted in millions with two decimal places:
0.00
0.08
1.68
Is this possible, or do I need to divide the numbers myself before applying the formatting?
Is this possible, or do I need to divide the numbers myself before applying the formatting?
You need to do the division. You can use CASE WHEN, by the way, if you aren't having numeric values to track what to divide by:
SELECT tablevalue / CASE divby WHEN 'thousand' THEN 1000 WHEN 'million' THEN 1000000 ELSE 1 END
I presume you'll have some column in your "format" table that also specifies what to divide by..
So you don't want to add a column.. you can store the info in the existing column.. you just have to work more to get it out:
SELECT
TO_CHAR(
somenumber / CASE RIGHT(format, 1) WHEN 'k' THEN 1000 WHEN 'M' THEN 1000000 END,
LEFT(format, -1)
)
So now you can make your format like 99D99k and the k will cause a divide by 1000 and the result is formatted to 99.99, so if you have 1234, format 9.99k you'll get '1.23'out of it.
If you want the [k] at the start it's just some jiggling of the LEFT and RIGHT functions..
TO_CHAR(
somenumber / CASE LEFT(format, 3) WHEN '[k]' THEN 1000 WHEN '[M]' THEN 1000000 END,
RIGHT(format, -3)
)

Want decimal result Amazon Redshift

I am trying to calculate the average ice creams a kid will have during summer.
I want the result to have 2 decimals.
Query:
Select day, (sum(ice_cream_cones) *1.0)/(select count(kids))
From t1 Group by 1
The result I get is something like 1.0003. I only want 2 decimal points.
Any suggestions?
You can do this with round
Select day, round((sum(ice_cream_cones) *1.0)/(select count(kids) ), 2)
From t1
Assuming your source data are integers...
SUM(ice_cream_cones) * 100 / COUNT(kids) / 100.0
Or, cast your existing calculation to a decimal?
CAST(<calc> AS DECIMAL(8,2))

divide 2 columns in DB2

How to divide 2 columns in DB2. For example I have the below query
select x.planned,y.unplanned,(x.planned /y.unplanned) average from
((select count(1) planned from
(select 1 from table
--------
union all
select 1 from table
--------)) x
join
(select count(1) unplanned from
(select 1 from table
--------
union all
select 1 from table
--------)) y
on 1=1)
This query brings output as
PLANNED UNPLANNED AVERAGE
25 6 4
The value in AVERAGE is incorrect it should display 4.16 instead of 4. I'm not sure what is the mistake.
25 / 6 equals 4⅙ it does not equal 4.16 unless you are explicitly rounding down the result to two decimal places.
For the most flexibility and precision when using decimal numbers in Db2 you could consider using use DECFLOAT. For example
$ db2 "values 25::decfloat/6"
1
------------------------------------------
4.166666666666666666666666666666667
When rounding DEFFLOAT values, Db2 uses the values of the CURRENT DECFLOAT ROUNDING MODE special register to determine the rounding mode. This defaults to ROUND_HALF_EVEN (and is the only values allowed in some Db2 configurations).
db2 "values ROUND(25::decfloat/6,2)"
1
------------------------------------------
4.17
1 record(s) selected.
to truncate (i.e. round down) then you can use DECIMAL and FLOATs as Mao suggested, or to avoid issues with binary floating points, just DECIMALs
$ db2 "values DECIMAL(25::DECIMAL/6,7,2)"
1
---------
4.16
1 record(s) selected.
You can use something like: CAST( float(x.planned)/float(y.unplanned) AS DECIMAL(9,2) ) as average to explicitly control the result precision and scale

Calculate percentage between two columns in SQL Query as another column

I have a table with two columns, number of maximum number of places (capacity) and number of places available (availablePlaces)
I want to calculate the availablePlaces as a percentage of the capacity.
availablePlaces capacity
1 20
5 18
4 15
Desired Result:
availablePlaces capacity Percent
1 20 5.0
5 18 27.8
4 15 26.7
Any ideas of a SELECT SQL query that will allow me to do this?
Try this:
SELECT availablePlaces, capacity,
ROUND(availablePlaces * 100.0 / capacity, 1) AS Percent
FROM mytable
You have to multiply by 100.0 instead of 100, so as to avoid integer division. Also, you have to use ROUND to round to the first decimal digit.
Demo here
The following SQL query will do this for you:
SELECT availablePlaces, capacity, (availablePlaces/capacity) as Percent
from table_name;
Why not use a number formatting function such as format_number (or an equivalent one in your database) to format a double as a percentage? This example is generalized. The returned value is a string.
WITH t
AS
(
SELECT count(*) AS num_rows, count(foo) as num_foo
FROM mytable
)
SELECT *, format_number(num_foo/num_rows, '#.#%') AS pct_grade_rows
FROM t
This avoids the use of round and multiplying the numerator by 100.