I'm trying to round a decimal and it seems like rounding in SQL Server only considers one digit after the last digit of rounding scale.
SELECT CAST(795.5921967249997 AS decimal(18,8))
--795.59219672
SELECT ROUND(795.5921967249997, 8)
--795.5921967200000
What I'm looking for is a fair rounding, that considers all digits. In example above I'm expecting 3 as 8-th decimal digit.
Can I do this without writing my own function?
If you want "3" in that position, then you are changing the rules of rounding. You can do this in two steps:
SELECT CAST(CAST(795.5921967249997 AS decimal(18, 9)) as decimal(18, 8))
Related
I have a data set with inconsistencies in a column with double values. Some are displayed as eg. 24.55, and others as 24.5 or 24. I want all values to be displayed to 2 decimals, so 24 should be 24.00, and 23.1 should be 23.10 etc. What code would work in this instance?
In general, such conversions are both database-specific and GUI-specific. However, the database can convert the value to something with two decimal places by using numeric/decimal (those are equivalent):
select cast(value as numeric(10, 2))
The "2" is the two digits after the decimal place. This should be displayed with two digits -- in any reasonable interface.
If you are using MySQL (as PHP suggests), you can use the format() function to accomplish this:
select format(value, 2)
I am trying to round a value in SQL, here is the code that I have:
select round(600.000,2)
How do I get the value 600.00?
Instead of round() convert to a decimal:
select cast(600.000 + 0.5 as decimal(10, 2) )
round() changes the value but it might not change the type of the result. Hence, you might still see extra decimal points (depending on the database and the application). Converting to a decimal with two digits of precision converts both the value and the type.
I am trying convert number values stored in the database as cents into dollar and keep 2 decimal places.
The following code will work only if v_cent/100.00 is NOT integer
SELECT CAST(ROUND(v_cent/100.00, 2) AS NUMERIC(8,2)) FROM DUAL;
If v_cent = 20000 then the result is 200.
How could I reserve 2 decimal places even if the result is integer?
Use to_char(), say to convert this to an output format with two decimal places:
select to_char(v_cent / 100.0, 'FM999999.99')
from dual;
As for your formulation it is doing the right thing. The only issue is that the decimal points are not printed out by default.
I need to concatenate between several fields (text and numeric) and It must be accurate. Some of the fields are originally Numeric(19,6) and I need it to be with only 2 digits after the decimal point.
I'm using the following queries and if I run no. 1 I get in the CONCAT_AMOUNT a rounded numbers like so: 38156.738156.7 and in no. 2 I get it correct - 476.47476.47.
Why is it happening and how can I solve this with minimum functions?
SELECT
38156.650000 AS AMOUNT,
CAST(38156.650000 as float),
CONCAT(cast(38156.650000 as float),
cast(38156.650000 as float)) AS CONCAT_AMOUNT
SELECT
467.47 AS AMOUNT,
CAST(467.47 as float),
CONCAT(cast(467.47 as float),
cast(467.47 as float)) AS CONCAT_AMOUNT
Okay, so SQL Server makes some assumptions when converting floating point values to strings. That shouldn't be surprising. The database cannot print out an infinite number of places after the decimal point.
So, two easy choices: convert to decimal or use str():
select concat(cast(38156.650000 as decimal(10, 2)) . . .
or
select concat(str(38156.650000, 10, 2) . . .
Note: the first version is SQL standard and should work in any database.
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