Length of input parameter greater than format mask To_CHAR - sql

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

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