SQL Roundup to 2 or 3 decimal - sql

SQL only have round to nearest function and roundup to nearest number.
How can I round up my prices, if price <1 roundup to 3 decimal and if price >=1 roundup to 2 decimal.
E.g
Price
$0.0024 round up to 0.003
$0.0051 round up to 0.06
$1.213 round up to 1.22
Thanks.

Use ceiling() to always round up:
select case when price < 1 then ceiling(price * 1000) / 1000
else ceiling(price * 100) / 100
end
from table

you can use round function with case below will work for sql server
select case when price<1 then round(price,3) else round(price,2) end
from table

There is 2 things to know :
ROUND() Always round DOWN :
ROUND(0.051, 2) = 0.05
If you want to round up, you need to use another function : CEILING()
But Ceiling only return an integer. The tricks is to do this :
CEILING(0.0051 * 1000)/1000 = 0.006
For the data you provide here is the solution :
SELECT
CASE
WHEN price < 1 THEN CEILING(price*1000)/1000
ELSE CEILING(price*100)/100
END AS rounded_price_up
FROM mytable

Related

PrestoSQL Why can't i divide an aggregating function inside a case statement?

I am getting null for some of the rows in Amazon Athena when the absolute difference is bigger than 20 percent. I am not sure I understand the logic of why. Can someone explain?
case
when gla_sqft = AreaBuilding then 0
when ((abs(AreaBuilding- gla_sqft)/gla_sqft) > 0.2) then 1 else null
end as percent_diff_logic
AreaBuilding
gla_sqft
percent_diff
percent_diff_logic
1498
2100
0.2866666666666667
Most likely you are getting null because there are rows where (abs (AreaBuilding-gla_sqft) / gla_sqft) is less than or equal to 0.2. Performing a quick select query (SELECT ... WHERE ((abs (AreaBuilding-gla_sqft) / gla_sqft) <= 0.2)) should eliminate my assumption.
It seems that you have standard case of integer division, i.e. 602/2100 is equal to 0. Just multiply result of abs by 1.0 or cast it to double:
when ((abs(AreaBuilding- gla_sqft) * 1.0 /gla_sqft) > 0.2) then 1 else null
or
when ((cast(abs(AreaBuilding- gla_sqft) as double)/gla_sqft) > 0.2) then 1 else null

I have divided 2 column in sql lite and received a output. However, the decimals are too long and I want only 2 decimals. How do I get that?

Below is the query which I have used. I need the result only 2 decimals not more than that.
:sum(CAST(CASE When t.issue_resolved like 'TRUE' then 1 else 0 END AS Float) * 100 /
count(t.issue_resolved) AS finalscore
t.issue_resolved (1 column)
2022-03-19 80.97826086956522 - (I want this to be only decimals)
You can use round function
round(sum(CAST(CASE When t.issue_resolved like 'TRUE' then 1 else 0 END AS Float) * 100 / count(t.issue_resolved),2) AS finalScore

SQL ending price with 5

I'm having a problem with a round.
I need to round prices to end with 5.
Example 1: 647,927 needs to end with 5 with tax. So it should be 815.
647,927 * 1,25 (tax) is 809,90 how can i round 809,90 up to 815
Example 2: 283,30 needs also to end with 5 with tax. So it should be 355.
283,30 * 1,25 (tax) is 354,125 how can i round 354,125 up to 355?
You can use arithmetic. Something like:
select ceiling( (val - 5) / 10 ) * 10 + 5
Here is a db<>fiddle for your two examples.

After inserting to the table, digits after decimal point changes to 0

While SELECT'ing columns in Vertica, it shows normal numeric values:
SELECT nvl2(exposure_time_ms, ROUND(exposure_time_ms / 1000, CASE WHEN exposure_time_ms < 10000 THEN 1 ELSE 0 END), 0) :: numeric(12,1) AS exposure_seconds
1
But when I am inserting the same thing to the table, which has column 'exposure seconds' type NUMERIC(12,1), it changes all digits after the decimal point to 0:
2
Presumably, this is because the nvl2() calculation produces an integer. So, try this:
SELECT nvl2(exposure_time_ms,
ROUND(exposure_time_ms / 1000.0,
CASE WHEN exposure_time_ms < 10000 THEN 1 ELSE 0 END
), 0
)::numeric(12,1) AS exposure_seconds
I will say that it really doesn't make sense to round the values based on the magnitude. You should store the value "as is" and use rounding for presentation -- if it is really needed.

Rounding up with two decimal places usind SQL

I want to a number like 3.5212, becomes 3.53. But using:
select Ceiling(3.5212)
it returns 4.
Can I make it always round up with two decimal places?
One way could be to multiply the value by 100 before ceiling function and then divide the final value by 100 as below.
select Ceiling(3.5212 * 100) / 100
Result:
VAL
--------
3.530000
DEMO