How to round a number including least significant zeros? - sql

I am trying to execute following SQL query in Oracle
Select round ( 123.50000065 , 4 ) from dual;
Output : 123.5
Required output: 123.5000
Any help is appreciated. ..

You probably want to use to_char with required format:
Below rounds the value to 4 decimal places and formats into the required string:
Select to_char(123.50000065, '999999999990D9999') x from dual;
If you don't want to actually round the number i.e. you just want to truncate after 4 digits, use:
Select to_char(trunc(123.50000065, 4), '999999999990D9999') x from dual;

ROUND ( numeric_expression , length [ ,function ] )
SELECT ROUND(123.9994, 3), ROUND(123.9995, 3);
Output:
123.9990 124.0000

Instead of round(), use to_char() or cast() to a decimal type:
select to_char(val, '999.9999'),
cast(val as decimal(10, 4))

To control the format a number is showed, you can cast it to a string, by applying the right format mask.
Depending on how you need round your input value, one of these could be useful:
with test(x) as (
select 123.50000065 from dual union all
select 123.00004 from dual union all
select 123.00005 from dual union all
select 123.00008 from dual
)
select x,
to_char(x, 'FM99999999.0000'),
to_char(trunc(x, 4), 'FM99999999.0000')
from test ;
result:
X TO_CHAR(X,'FM9 TO_CHAR(TRUNC(
-------------------------- -------------- --------------
123,50000065000 123.5000 123.5000
123,00004000000 123.0000 123.0000
123,00005000000 123.0001 123.0000
123,00008000000 123.0001 123.0000

"Rounding" is a mathematical concept. The value (with your sample input) is 123.5. Mathematically 123.5000 is the same thing as 123.5. They are only different as STRINGS.
One way to display 123.5 as 123.5000 is to wrap round() within to_char(). However, this means you are not able to use it in further computations (actually Oracle will allow you to - it will do an implicit conversion back to number instead of throwing a data type mismatch error, as it should do).
The better way, in most cases, is to address formatting in your client software, like SQL Developer, SQL*Plus, or Toad. Here is how you can do it in SQL*Plus:
SQL> Select round ( 123.50000065 , 4 ) as result from dual;
RESULT
----------
123.5
-- change the format of the numeric column "result"
SQL> column result format 999.0000
SQL> Select round ( 123.50000065 , 4 ) as result from dual;
RESULT
---------
123.5000

I can't see how you got 123.5 from your query. mine results 123.50000000
if I understand correctly, you want your number 4 significant decimal places.
why not try cast
select cast(123.50000065 as numeric(38,4))
output: 123.5000
testing if it rounds off number:
select cast(123.50000065 as numeric(38,6))
output: 123.500001

Related

Placing a decimal point just before last two digit of a number

I have a query as below
|| LPAD (TRIM (TO_CHAR (RWTEXPT_STD_AMOUNT, 'FM9999999999999D00')), 15, '0')
its giving the result : 000011545467.00
what i need is : 000000115454.67
i have tried 'FM9999999999999D00' and '999999999999D99' but it gives the same results 000011545467.00
what i need is 000000115454.67
Convert your string value to a number, divide it by 100 and then format it:
SELECT TO_CHAR(
TO_NUMBER(RWTEXPT_STD_AMOUNT)/100,
'FM000000000000D00'
) AS result
FROM table_name
Which, for the sample data:
CREATE TABLE table_name (RWTEXPT_STD_AMOUNT) AS
SELECT '000000011545467' FROM DUAL;
Outputs:
RESULT
000000115454.67
fiddle
Just do it this way
Select To_Char(14.5, 'FM000000000000D00') "NMBR" From Dual
--
-- NMBR
-- ----------------
-- 000000000014.50
Put any number of leading zeros within the format. Zero means zero and 9 is a placeholder if there is a number present. Letter D is for decimal point character. You can use G for grouping character too.
Regarding decimals - if your column has integer value like 1467 and you know that last two numbers are decimal numbers then just put 1467/100
Select To_Char(1467/100, 'FM000000000000D00') "NMBR" From Dual
--
-- NMBR
-- ----------------
-- 000000000014.67
Regards...

How to remove trailing zeros after decimal points from number column in sqlplus?

I have a one database table field called Amount which type is number(38,8), and in sqlplus I have formatted the column like "COLUMN Amount FORMAT 999999999999999999999999.99999",but while writing into csv we are getting always trialing zeros.
e.g if number is 9.23 then result is will be 9.2300
e.g if number is 9 then result is will be 9.0000
How to remove trailing zeros.
Please help me..
You can do it with to_char and the format-code TM (see documentation)
SELECT to_char(column, 'TM') FROM table;
examples:
SELECT to_char(9.2300, 'TM') FROM dual; -- returns 9.23
SELECT to_char(9.0000, 'TM') FROM dual; -- returns 9
SELECT to_char(100, 'TM') FROM dual; -- returns 100
SELECT to_char(010, 'TM') FROM dual; -- returns 10
edit:
With
SELECT round(to_char(column, 'TM'), 5) FROM table;
you can limit your result to 5 decimal places.
SELECT to_char(round(123.654321000, 5), 'TM') FROM dual; -- returns 123.65432
TO_NUMBER function also removes the zeroes after decimal point.
eg : 1.4500 to 1.45, 1.9000 to 1.9 etc.
Sample Query :
SELECT TO_NUMBER(column_name) FROM table_name;
Try this.
select rtrim(rtrim(to_char(column_name), 0 ), '.' ) from table_name;

Decimal numbers and currency formatting in Oracle

select .5 as colm from dual;
gives 0.5 as the output
But
select .5||'$' as colm from dual;
gives .5$ as the result. Why I am not getting 0.5$
How can I achieve 0.5$ in the second query?
Instead of appending, we can do like below. Format the currency in expected format.
select TO_CHAR(.5,'FM999999990.099999999$') as colm from dual;
Otherwise, you still can concat the currency symbol like,
select TO_CHAR(.5,'FM999999990.099999999')||'$' as colm from dual;
Key is you have to explicitly mention the number format before concatenating.
Format models are well-covered in the documentation. Read it here.
Oracle Setup:
CREATE TABLE your_table ( value ) AS
SELECT 0.5 FROM DUAL UNION ALL
SELECT 5 FROM DUAL;
Query:
SELECT value,
REGEXP_REPLACE(
TO_CHAR( value, 'FM9999999990.99' ),
'\.$'
) || '$' AS formatted_value,
CASE
WHEN TRUNC( value ) = value
THEN TO_CHAR( value, '9999999990' )
ELSE TO_CHAR( value, 'FM9999999990.99' )
END || '$' AS alternate_value,
TO_CHAR( value, '9999999990.99L' ) AS currency_value
FROM your_table;
Output:
VALUE FORMATTED_VALUE ALTERNATE_VALUE CURRENCY_VALUE
----- --------------- --------------- --------------
0.5 0.5$ 0.5$ 0.50$
5 5$ 5$ 5.00$
Do you like to write $ as a constant string or is it your local currency symbol (US-Dollars)?
In later case, this solution may work for you:
SELECT TO_CHAR(0.5555, 'fm999999990D99L') from dual;
See TO_CHAR - Number Format Elements:
L
Returns in the specified position the local currency symbol (the
current value of the NLS_CURRENCY parameter).
You cannot concatenate a "NUMBER" field with a String field.
In your case .5 is a number and '$' is a string. So if you want your result to be '0.5$' you have to pass 0.5 as a string as below:-
select '0.5'||'$' as colm from dual;

How to select numbers with precision in Oracle

I have a query and in some of columns, it returns numbers, like 1,2,3 and etc but I need to show them as 1,00, 2,00, 3,00 respectfully, how can I achieve it? I tried
CAST(2 as FLOAT(2))
and
ROUND(2, 2)
but those didn't work. I also tried to use to_char, but I didnt understand how to use masking.
Assuming that your goal is to select a character string that represents the number and that you always want 2 decimal places to be displayed and for the string to respect your session's NLS settings for the decimal separator (whether that is a comma or a period)
select to_char( <<the number>>, '0D99' )
from <<your table>>
This will print a leading 0 as well if the number is less than 1. If you don't want a leading 0, use the "9D99" format mask
Set your nls_numeric_characters according to your requirement for the separator. Use TO_CHAR to format the output.
SQL> alter session set nls_numeric_characters=',.';
Session altered.
SQL> WITH DATA(num) AS(
2 SELECT 1 FROM dual UNION ALL
3 SELECT 2 FROM dual UNION ALL
4 SELECT 3 FROM dual
5 )
6 SELECT to_char(num, '9D99') FROM DATA;
TO_CH
-----
1,00
2,00
3,00
SQL>

Change number format by adding decimal to it in oracle

I have a number 000005500 and I want it to be in format 0000055.00 or 55.00 using an Oracle select query.
I used this query:
select to_char(to_number(000005500),'99.99') from dual
but its displaying #####
How can I display it in the format I need?
As written your to_number() call is just doing an implicit conversion to a string and then an explicit conversion back to a number, which seems pointless, but I assume you're actually dealing with a value from a varchar2 column. In which case you see:
select to_char(to_number('000005500'),'99.99') from dual
TO_CHA
------
######
You're seeing the hashes because you can't fit your four-digit number, 5500, into a 99.99 format - you have four digits before the decimal point and the format mask only allows for two.
The bit you seem to be missing is dividing by 100 to get the decimal:
select to_char(to_number('000005500') / 100,'99.99') from dual;
TO_CHA
------
55.00
Another approach, if you want to keep it as a string with the same number of leading zeros as the oroginal value, is to leave it as a string, chop it up with substr(), and concatenate the parts back together. Using a CTE as a demo:
with t as (select '000005500' as val from dual)
select val, substr(val, 1, length(val) - 2)
|| '.' || substr(val, length(val) - 1, 2) as adj_val
from t;
VAL ADJ_VAL
--------- ---------------------------------------------
000005500 0000055.00
Firstly, 000005500 is not a number. A number doesn't start with zero. You are dealing with a string.
I want it to be in format 0000055.00
You can only expect it to be a string as an output, and not a number.
Anyway, to get the output as 55.00 as NUMBER, you could do the following -
SQL> WITH DATA AS(
2 SELECT '000005500' num FROM DUAL
3 )
4 SELECT to_char(to_number(replace(num,'0','')),'99D99') FROM DATA
5 /
TO_CHA
------
55.00
SQL>
Or,
SQL> WITH DATA AS(
2 SELECT '000005500' num FROM DUAL
3 )
4 SELECT to_char(to_number(rtrim(ltrim(num,'0'),'0')),'99D99') FROM DATA
5 /
TO_CHA
------
55.00
SQL>
Edit
Alex's method is also nice, it uses simple mathematics to convert it to DECIMAL. I would prefer his way for the first part.
Another couple of alternatives around the final formatting:
select to_char(to_number('000005500')/100,'0999999D90') leading_zeros,
to_char(to_number('000005500')/100,'fm9999999D90') no_leading_zeros
from dual;