Oracle 11.2 to_number multiple commas - oracle

In Oracle 11.2, is there some number format, nf, that will work with to_number to parse arbitrary length varchar2s containing digits and commas?
I can achieve this without a number format, by using regexp_replace, but I'd prefer to achieve the same thing using just a number format.
e.g., the following 2 statements work:
select to_number(regexp_replace('12,345', ',', '')) from dual;
select to_number(regexp_replace('1,234,567', ',', '')) from dual;
but I'd prefer:
select to_number('12,345', nf) from dual;
select to_number('1,234,567', nf) from dual;
where nf is one number format string that works for both statements.
If I try nf = '99,999', the first statement works, but the second fails.
Thanks.

Oracle won't complain if the number format is too long, so you can use a model that has enough digits to cope with the biggest number you can receive:
SQL> select to_number('12,345',
2 '999G999G999G999G999G999G999G999G999G999G999G999G999') from dual;
TO_NUMBER('12,345','999G999G999G999G999G999G999G999G999G999G999G999G999')
-------------------------------------------------------------------------
12345
SQL> select to_number('1,234,567',
2 '999G999G999G999G999G999G999G999G999G999G999G999G999') from dual;
TO_NUMBER('1,234,567','999G999G999G999G999G999G999G999G999G999G999G999G999')
----------------------------------------------------------------------------
1234567
SQL> select to_number('999,999,999,999,999,999,999,999,999,999,999,999,999',
2 '999G999G999G999G999G999G999G999G999G999G999G999G999') from dual;
TO_NUMBER('999,999,999,999,999,999,999,999,999,999,999,999,999','999G999G999G999
--------------------------------------------------------------------------------
1.0000E+39
I've used the G group separator instead of a fixed comma to support globalisation, but the effect is the same.
The only caveat is that the source number has to have the right grouping so it matches the formatting exactly for the digits it does have:
SQL> select to_number('1,2345',
2 '999G999G999G999G999G999G999G999G999G999G999G999G999') from dual;
select to_number('1,2345',
*
ERROR at line 1:
ORA-01722: invalid number

Although I support Alex Poole's answer, here's another crude but effective way of solving the problem that should perform better than doing a regex.
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE table_of_numbers (
example_num VARCHAR2(50)
)
/
INSERT INTO table_of_numbers (example_num)
VALUES ('12,345')
/
INSERT INTO table_of_numbers (example_num)
VALUES ('1,234,567')
/
Query 1:
SELECT TO_NUMBER(example_num, RPAD('9', LENGTH(example_num) - 1, '9')) fudge
FROM table_of_numbers
Results:
| FUDGE |
-----------
| 12345 |
| 1234567 |
If you need to match the commas, then you could do something slightly more sophisticated with INSTR and LPAD to make sure you generate the right mask.

For this :
select to_number('1,234,567', nf) from dual;
Use nf = 9,999,999 will work.

Related

ORA-12899: value too large for column - on export/import, both BYTE semantics, not CHAR

I have an Oracle DB on one Windows machine and perform an export of all tables + contents using SQL developer 4.0.2.15. File format is plain text SQL, ANSI ISO-8859-1. The line endings are done in UNIX style (1 byte).
The table / column definitions are like:
"PRODUCTNAME" VARCHAR2(120 BYTE),
When I execute this .sql file on the target Windows machine, tables are created and inserts are done. But some items that have exactly 120 bytes in the PRODUCTNAME (including 1 line feed character) fail to insert on the target machine.
I get:
ORA-12899: value too large for column "DBNAME"."TABLENAME"."PRODUCTNAME" (actual: 121, maximum: 120)
I don't understand why. I have no 2-byte characters in this particular string, and the export is definitely done using 0x0A as line break character (checked via Notepad++ status line and its HexView Plugin). I use drag&drop to open the .sql file in the target machine's SQL developer (same version). When I copy the productname with its line break into Notepad++ via clipboard, it count's 120 bytes of length.
I do not understand, why in Oracle developer the script counts this 1 extra character.
I have searched for this in google and found topics here on SO among others, but they don't help me or I don't fully understand them.
What do I miss? Please help!
Create a temporary table (say, TEST) having PRODUCTNAME VARCHAR2(200). Import data into it. Check rows that exceed length of 120 characters.
You might need to use the DUMP function, such as
select dump(productname) from test
It'll show you actual length of data stored in that column. Just for example:
SQL> select ename, dump(ename) dump_e from emp where rownum = 1;
ENAME DUMP_E
---------- ----------------------------------------
KING Typ=1 Len=4: 75,73,78,71
-----
this!
SQL>
[EDIT: TRIM example]
SQL> with test (id, col) as
2 (select 1, 'abc' from dual union
3 select 2, 'def ' from dual union
4 select 3, ' ghi ' from dual
5 )
6 select '#' || col || '#' col,
7 '#' || trim(col) || '#' new_col
8 from test
9 order by id;
COL NEW_COL
-------- --------
#abc# #abc#
#def # #def#
# ghi # #ghi#
SQL>

Why oracle doesn't show the integer part of a decimal

when I execute the next SQL:
select 1/3 from dual;
the result is ,333333333
but the result that I expected was 0,333333333
Why doesn't oracle show the zero ? is there any parameter that can affect it?
Thanks
Edit: I checked this behaviour from an aplication that uses the oo4o (oracle object for ole), and confirmed with the sqlDeveloper (when I execute the sql with F5, not with ctrl+intro). The real problem is with the oo4o Dynaset object: when I try to sum the value of two fields, what I get is a concatenation, not a sum: ,3 + ,2 = ,3,2.
What I want to know is if there is some parameter or configuration that makes the oracle engine return the number without the zero, so I can turn on|off in order to return de zero as integer part.
SQL*Plus will show that by default (using your territory's decimal separator):
SQL> select 1/3 from dual;
1/3
----------
.333333333
You could use set numformat to change the behaviour:
SQL> set numformat "0D9999"
SQL> select 1/3 from dual;
1/3
-------
0.3333
... where the D represents the decimal separator. Or you could use column formatting, with a column alias:
SQL> set numformat ""
SQL> column answer format 0.000
SQL> select 1/3 as answer from dual;
ANSWER
------
0.333
Other clients have different ways of controlling the default output; SQL Developer behaves much the same, but PL/SQL Developer, Toad etc. might not.
Or you can format the number as part of the query, which isn't client-dpendent:
SQL> select to_char(1/3, '9990D99999') from dual;
TO_CHAR(1/3
-----------
0.33333
You need to provide enough digits for the integer part of whatever you're calculating though. Anything less than zero is simple, but if there are too many digits before the decimal separator then it won't display at all:
SQL> select to_char(100000/3, '9990D99999') from dual;
TO_CHAR(100
-----------
###########

How to put double quote in oracle output records

This is a oracle output records with pipe delimiter
04/22/2015|695|1074795|CRUSE|AXDE|01/29/1963|88359|||||
I want to change like this
"04/22/2015"|"695"|"1074795"|"CRUSE"|"AXDE"|"01/29/1963"|"88359"|||||
What is the query in Perl?
I want to change like this
"04/22/2015"|"695"|"1074795"|"CRUSE"|"AXDE"|"01/29/1963"|"88359"|||||
I won't say this is an elegant way to do it, since it is always better to fix the source itself. In your case, whatever is generating the output, you could simply concatenate the double-quotation marks to the column names.
Anyway, you could do it in SQL using REPLACE and RTRIM:
SQL> WITH DATA AS(
2 SELECT '04/22/2015|695|1074795|CRUSE|AXDE|01/29/1963|88359|||||' str FROM dual
3 )
4 SELECT '"'||rtrim(REPLACE(REPLACE(str, '|', '"|"'), '""',''),'"') str FROM DATA;
STR
---------------------------------------------------------------------
"04/22/2015"|"695"|"1074795"|"CRUSE"|"AXDE"|"01/29/1963"|"88359"|||||
SQL>
check this
select '"04/22/2015"|"695"|"1074795"|"CRUSE"|"AXDE"|"01/29/1963"|"88359"|||||' as str from dual
STR
---------------------------------------------------------------------
"04/22/2015"|"695"|"1074795"|"CRUSE"|"AXDE"|"01/29/1963"|"88359"|||||

Oracle cursor removes leading zero

I have a cursor which selects date from column with NUMBER type containg floating point numbers. Numbers like 4,3433 are returned properly while numbers smaller then 1 have removed leading zero.
For example number 0,4513 is returned as ,4513.
When I execute select used in the cursor on the database, numbers are formatted properly, with leading zeros.
This is how I loop over the recors returned by the cursor:
FOR c_data IN cursor_name(p_date) LOOP
...
END LOOP;
Any ideas why it works that way?
Thank you in advance.
You're confusing number format and number value.
The two strings 0.123 and .123, when read as a number, are mathematically equals. They represent the same number. In Oracle the true number representation is never displayed directly, we always convert a number to a character to display it, either implicitly or explicitly with a function.
You assume that a number between 0 and 1 should be represented with a leading 0, but this is not true by default, it depends on how you ask this number to be displayed. If you don't want unexpected outcome, you have to be explicit when displaying numbers/dates, for example:
to_char(your_number, '9990.99');
It's the default number formatting that Oracle provides.
If you want to specify something custom, you shall use TO_CHAR function (either in SQL query or PL/SQL code inside the loop).
Here is how it works:
SQL>
SQL> WITH aa AS (
2 select 1.3232 NUM from dual UNION ALL
3 select 1.3232 NUM from dual UNION ALL
4 select 332.323 NUM from dual UNION ALL
5 select 0.3232 NUM from dual
6 )
7 select NUM, to_char(NUM, 'FM999990D9999999') FORMATTED from aa
8 /
NUM FORMATTED
---------- ---------------
1.3232 1.3232
1.3232 1.3232
332.323 332.323
.3232 0.3232
SQL>
In this example, 'FM' - suppresses extra blanks, '0' indicates number digit including leading/trailing zeros, and '9' indicates digit suppressing leading/trailing zeros.
You can find many examples here:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34570

Oracle statement

Tables records as below
D:\HOME\DOC\FILE\2001\12\TT-12S2Q99-EE-EE1.pdf
D:\HOME\DOC\FILE\2002\02\TT-12S2Q94-EE-EE1.xml
D:\HOME\DOC\FILE\2005\05\TT-12S2Q98-EE-EE1.pdf
D:\HOME\DOCS\TEMPFILE\TT-12S2Q88-EE-EE1.pdf
I want to extract those file names only. The result should be
TT-12S2Q99-EE-EE1
TT-12S2Q94-EE-EE1
TT-12S2Q98-EE-EE1
TT-12S2Q88-EE-EE1
Anyone can give me a hand ?
You can use INSTR with the third parameter negative to search backwards in the string from the end (something I didn't know you could do until a few minutes ago). Combine that with a SUBSTR and you should have what you want:
SQL> select filename from mytable;
FILENAME
--------------------------------------------------------------------------------
C:\path\to\some\file.txt
SQL> select substr(filename, instr(filename, '\', -1) + 1) from mytable;
SUBSTR(FILENAME,INSTR(FILENAME,'\',-1)+1)
--------------------------------------------------------------------------------
file.txt
Take a look at the SUBSTR and INSTR functions.
If you're still stuck after that, post your best attempt at what you've done.

Resources