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;
Related
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...
https://dbfiddle.uk/?rdbms=oracle_18&fiddle=94771b6589b01526ad0cf6e5c4d01945
I need help in extracting the number substring from a file name
currently for file format - 'monkey_eats_mango_everyday_202002.txt'
we are doing like this
select regexp_substr('monkey_eats_mango_everyday_202002.txt', '\d+') as parameter12a
from dual;
result-
202002
which in turn used in larger query to get the last date of this date like this
select to_char(last_day(to_date(regexp_substr('monkey_eats_mango_everyday_202002.txt', '\d+'), 'yyyymm')), 'yyyymmdd') as parameter
from dual ;
result-
20200229
Now the file format has changed, so we have - 'donkey_eats_pines_cones_20192301_7771234_everyday_202002.txt'
In this file format there are numbers at other places like 201943_7771234 which can be dates or any random number, so I need regex expression which can extract 202002 from file format
select regexp_substr('donkey_eats_pines_cones_201943_7771234_everyday_202002.txt', '\d+') as parameter12a
from dual;
You can use a \. to anchor your digits match to next to the period in the file name, and then use a capture group around the digits to get just the digits in the output, using the 6th parameter to REGEXP_SUBSTR to indicate that you only want group 1 in the output:
SELECT REGEXP_SUBSTR('donkey_eats_pines_cones_201943_7771234_everyday_202002.txt', '(\d+)\.', 1, 1, NULL, 1) AS parameter12a
FROM dual;
Output:
202002
Demo on dbfiddle
One option is to use nested expressions: inner returns file extension and the date (that precedes that extension), and outer fetches date itself.
SQL> with test (col) as
2 (select 'donkey_eats_pines_cones_201943_7771234_everyday_202002.txt' from dual)
3 select regexp_substr(regexp_substr(col, '\d+.\w+$'), '\d+') result From test
4 /
RESULT
------
202002
SQL>
check this
select reverse(split_part(reverse(r.r ), '.', 2)) from
(
SELECT reverse(split_part(reverse('donkey_eats_pines_cones_20192301_7771234_everyday_202002.txt'), '_', 1)) as r
)as r
ANS :
202002
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;
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
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>