sql server rounding depending on decimal value - sql

I have a decimal (18,2) column and when I do my select would like for it to round up/down depending on the decimal value.
Example:
value
1.50 -> 2
1.25 -> 1
so the basic decimal rule: >= .5 rounds up and < .5 round down
I've seen "Ceiling" round ups and "Floor" rounds down but I need to do both depending on the value. Any ideas?

You can use the ROUND() Function something like this....
SELECT ROUND(Value,0)
SQL FIDDLE

You need to use round function to round the column value in select statement. Please try following
select ROUNDUP(COLUMN_NAME, 0) FROM TABLE_NAME

Related

Converting decimal values to non decimal in computed column in oracle sql

I am trying to convert decimal values into non decimal number for a computed column in Oracle SQL.
Please see the below code. But I am not able to get the desired output.
Query:
Select cast(cost_account)*100/cast(amnt_fin) as "computed_LTV"
From Loan_app.
Here I Want a new column name as computed_LTV with the required calculation with no decimal in the output.
It is ROUND you're looking for, I presume.
SELECT ROUND (cost_account * 100 / amnt_fin) AS "computed_LTV" FROM Loan_app
If by "non-decimal number" you mean "integer", just use trunc():
Select trunc(cost_account * 100 / amnt_fin) as computed_LTV
From Loan_app;

Rounding of Decimal values

Is there any function in sql which rounds of the value to nearest whole number.
For example,
If value is 12.56 then it should be 13.
If value is 12.34 then it should be 12.
I tried with Ceiling and floor but it doesn't give me accurate result. Ceiling gives me both the value as 13 where as i need 13 and 12 depending on my value.
Is there any function that i can use to get the desired result I want?
Any help is appreciated!
I figured it out.
I just used ROUND(value,0) which gave me desired result.
If you don't care about your decimal values being saved, like the people said ROUND() will work, just pass the second parameter as 0 to say that you don't care about the decimal places, Precision = 0 = NO Decimal, so for example:
DECLARE #floater FLOAT = 2.50
SELECT ROUND(#floater, 0) --Returns 3
SET #floater = 2.10
SELECT ROUND(#floater, 0) --Returns 2

SQL Division precision

I have 2 columns which I need to divide sum(cola)/sum(ColB), but I am not getting the desired results since SQL server seems to truncate values after decimal
For eg. I have-
select 281370/1035
is giving 271 using simple division, whereas actual result of division is 271.8550724637681 and I want to display 271.8
I tried
SELECT cast(round(281370/1035,1) as numeric(36,1))
but that results 271.0
In SQL Server, you have to cast the integers to decimal and you could use Round to get desired precision.
SELECT cast(Round(CAST(281370 AS decimal) / CAST(1035 AS decimal),1,1) as decimal(10,1))
The problem is that you given the int number and want a decimal result
try this
select convert(decimal(30,10),281370.0/1035.0)
or
select Round(convert(decimal(30,10),281370.0/1035.0),1,1)
#Stormcloak gives the answer to specifically wanting a single position as a mantissa, however to return an exact answer you could "simply" implicitly change the datatype.
select 281370.0/1035
Returns:
271.855072
In Presto DB:
select (CAST(11 as decimal(8,6))/CAST(7 as decimal(8,6))) as result
result:1.571429
decimal(xp,xs)
xp--> total number of digits(before decimal point+ after decimal
point)
xs--> number of digits after the decimal point
reference: https://prestodb.io/docs/current/functions/decimal.html

sql server round

select round((cast(56/3 AS DECIMAL (4,2))),1)
is showing 18 output instead of 19 , where as actual value is 18.66.
My Round function is not working
please help.
The problem is 56/3 is an integer calculation which means no floating point numbers.
You need to use floating point numbers in the initial calculation e.g. 56.0/3. Also, if you want to round to 19 then you need to round to the nearest whole number, ROUND(x, 1) will round to the first decimal place - you need to pass 0 to round up to 19.
SELECT ROUND((CAST(56.0/3 AS DECIMAL (4,2))),0)
Alternatively, you could switch ROUND for CEILING
select CEILING(CAST(56.0/3 AS DECIMAL(4,2)))
Your section of the code:
CAST( 56/3 AS DECIMAL )
First evaluates the 56/3 which returns 18. This is then cast to decimal, giving 18.0.
You need to cast either the numerator or denominator before the division occurs.
The round function is working fine -- it's just not using the input you think it is.
Convert one of your integer values before you divide the numbers:
select round(56/convert(DECIMAL (4,2),3),0);
If you do not so you divide integers which results in 18 not 18.66

Use Group By for real or float column with specify precision

How can I specify precision for a column of the REAL type (eg. to three digits after the decimal point) for Group By clause?
Eg. 12.023006 is equal to 12.023007 for 3 digits precision after the decimal point and should be grouped by Group By.
A couple ways you can achieve this, one of which is to CAST your field to the appropriate precision:
select cast(somecol as decimal(10,3))
from sometable
group by cast(somecol as decimal(10,3))
SQL Fiddle Demo
You could also use the ROUND function to achieve this.