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

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;

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...

Length of input parameter greater than format mask To_CHAR

While executing the below query, the result is #.
Can someone explain to me the reason?
select to_char(1228.0000,'$999.9999') from dual;
Length of 1228 exceeds length of 999. The following works:
select to_char(1228.0000,'$9999.9999') from dual; -- $1228.0000
select to_char(122.0000,'$999.9999') from dual; -- $122.0000

How to round a number including least significant zeros?

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

Using Concat Operator ( || ) the Integer Part of is not shown for floating numbers

When I am writing a SELECT query in Oracle using concat || operator to concatenate a decimal number and a string. Then for values that have integer part as zero is not shown.
But when I am only selecting Number the Integer part is shown.
My SQL Query Structure :-
SELECT
NUMBER_VAL ||
STRING_VAL
FROM MY_TABLE;
for E.g (selecting only Number):-
SELECT 0.1 FROM DUAL;
I am getting Result: 0.01
E.g (Selecting Number and String Concatenated) :-
SELECT 0.01 || 'ABCD' FROM DUAL;
I am Getting Result: .01ABCD
Desired Result: 0.01ABCD
Can anyone help me how can I achieve the desired result.
This is in Addition to Rics Answer .
The Data type of NUMBER_VAL was NUMBER(9,4).
And that's why I had to Use the Format '99990.9999'
Use to_char function and the appropriate number format:
SELECT
to_char(NUMBER_VAL,'0.99') ||
STRING_VAL
FROM MY_TABLE;
You can specify format when converting to string, such as:
SELECT TO_CHAR(0.01, '0.00') || 'ABCD' 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>