SQL keep two decimal places for integers after converting cent into dollar - sql

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.

Related

SQL , format all numbers to 2 decimal places (eg 20 to 20.00)

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 want my data upto 6 decimal places in impala

I have a double type column in impala
while I am trying to cut it upto some decimal places
I got this error
ERROR: AnalysisException: No matching function with signature: truncate(DOUBLE, TINYINT).
e.g select truncate(cast(0.4893617021276596 as double),7);
any workaround will be welcome
You can use round():
select round(col, 6)
If you actually want a truncate, then subtract 0.0000005:
select round(col - 0.0000005, 6)
Using the DECIMAL type, it is possible to represent numbers with greater precision than the FLOAT or DOUBLE types can represent.
The maximum allowed precision and scale of the DECIMAL type are both 38.
Precision is the total number of digits, regardless of the location of the decimal point.
Scale is the number of digits after the decimal place.
To represent the number 8.54 without a loss of precision, you would need a
DECIMAL type with precision of at least 3, and scale of at least 2.
Example:
Note that the DECIMAL(17,16) type means there is a total of 17 digits, with 16 of them after the decimal point.
DECIMAL(17,16) 3.1415926535897932
You could ALTER your table with DECIMAL type as follow:
ALTER TABLE my_table CHANGE field field DECIMAL(precision, scale);
or as suggest #Gordon Linoff, you could use round() function.

Divide integers and round to 3 decimal places with one cast?

Is there a way to divide two integers and round to 3 decimal places with only one cast/convert?
All the examples I see convert to decimal, then round. Some cast both numerator and denominator, etc. I've seen round with floor or ceiling, etc.
I just want to use one cast and I'm done.
For example, 1/3 would be 0.333. 5/3 is 1.667
I'm doing this to cast that result as varchar.
Dividing two int values t-sql has only two possible outcomes:
error 8134 (division by zero)
an int value again
It should be sufficient to coerce one of the operands to numeric:
select round(1.0/3,3,0)
To avoid trailing zeroes, another way:
select cast(1.0/3 as decimal(18,3))
This of course works only with numeric literals. If you have a column or parameter value, a cast is still needed.
This feeble attempt at code golf shaves off one more character:
select convert(decimal(9,3),1.0/3)

Round a value to two decimal places in SQL

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.

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"