Rounding of Decimal values - sql

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

Related

Using LEFT with LEN Turns Float Into Weird Format (e.g. 6.0871e)

I have about 70 million rows of data with a column that contains numbers, but it's in a float format. I need to get rid of the last 4 digits of that column i.e. I need to turn this
60871003002001
60871003002002
60871003002003
into this
6087100300
6087100300
6087100300
When I run the query
select top 3 LEFT(COLUMN, LEN(COLUMN)-4) as a from TABLE
it returns the following:
6.0871e
6.0871e
6.0871e
Does anyone know why? I'm using SQL Server. There are no nulls and each number is from 12 to 15 digits long.
Thank you!
Instead, divide by 1000 and turn into a decimal:
select cast( (col / 10000) as decimal(18, 0))
The problem you are facing is that the default conversion of a float to a string might sometimes be in scientific notation.

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

sql server rounding depending on decimal value

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

Oracle: Decimal and rounding

I have a value in the table 0.0821, and I want to convert it to 8.21.
My SQL query is to_char(round(prdll_yr2.rate_chg_pct_qty *100 ),'9.99')
But it returns 8.00 instead of 8.21.
to_char(round(0.0821 *100,2 ),'9.99')
to_char(round(prdll_yr2.rate_chg_pct_qty *100,2 ),'9.99')
you're missing the number of decimals to display on the round... it will default to 0 if omitted
or for an example:
select to_char(round(0.0821 *100,2 ),'9.99') from dual;
Results in: 8.21
select to_char(round(0.0821 *100),'9.99') from dual;
Results in: 8.00
----------------------------GIVEN NEW INFORMATION:---------------------------
to_char(round(0.0821 *100,2 ),'9,999.99')
Adjust the 9,999.99 format to be equal to the scale and precision of the allowed in the database. so if your value is Numeric(9,5) this implies 4 leading numbers followed by 5 decimal places. Since you're multiplying by 100 the largest value you could have is 6 positions before the decimal so format of 999,999.99 and the 3rd decimal would be "Rounded"