ORA-01722: invalid number rows start with comma transfer VARCHAR2 TO_NUMBER - sql

I have following source data in VARCHAR2 format
,00100000004749745
,100000001490116
,125
,200000002980232
,25
,439999997615814
,5
0
1
1,10000002384186
1,5
100
2,1800000667572
3
3,29999995231628
96
999
What is the formula to transfer it to NUMBER?
With the following
INSERT INTO table_b.column_b
SELECT
TO_NUMBER (column_a,'9999999999D9999999999999999999999',
'nls_numeric_characters= ''.,''') as my_numbers
FROM table_a.column_a;
I get an error
ORA-01722: invalid number error message.
I assume that it is because rows starting with comma e.g (,125).
In destination table I need in number format the data like this
0,00100000004749745
0,100000001490116
0,125
0,200000002980232
0,25
0,439999997615814
0,5
0
1
...
Also tried to put zero '0' in front of comma and them change it to number with
Select
column_a,
TO_NUMBER (column_a,'9999D9999999999999999999999',
'nls_numeric_characters= ''.,''') as my_number
from
(SELECT DISTINCT
'0'|| column_a
FROM table_a.column_a
WHERE column_a LIKE (',125')
);
but the result was
0,125 125

As Vasyl stated, your nls_numeric_characters needs to be adjusted. The query below demonstrates how to convert the string to a number.
WITH
my_numbers (column_a)
AS
(SELECT ',00100000004749745' FROM DUAL
UNION ALL
SELECT ',100000001490116' FROM DUAL
UNION ALL
SELECT ',125' FROM DUAL
UNION ALL
SELECT ',200000002980232' FROM DUAL
UNION ALL
SELECT ',25' FROM DUAL
UNION ALL
SELECT ',439999997615814' FROM DUAL
UNION ALL
SELECT ',5' FROM DUAL
UNION ALL
SELECT '0' FROM DUAL
UNION ALL
SELECT '1' FROM DUAL
UNION ALL
SELECT '1,10000002384186' FROM DUAL
UNION ALL
SELECT '1,5' FROM DUAL
UNION ALL
SELECT '100' FROM DUAL
UNION ALL
SELECT '2,1800000667572' FROM DUAL
UNION ALL
SELECT '3' FROM DUAL
UNION ALL
SELECT '3,29999995231628' FROM DUAL
UNION ALL
SELECT '96' FROM DUAL
UNION ALL
SELECT '999' FROM DUAL)
SELECT n.column_a,
TO_NUMBER (n.column_a,
'9999999999D9999999999999999999999999999',
'nls_numeric_characters= '', ''') AS column_a_as_number
FROM my_numbers n;

Looks like you use wrong nls_numeric_characters values. Try to replace 'nls_numeric_characters=''.,''' with 'nls_numeric_characters='', '''
Explanation: Your nls_numeric_characters defined . as a decimal delimiter and , as a group delimiter, but, according to your example, you assume that the decimal delimiter is ,

Related

Find value that is not a number or a predefined string

I have to test a column of a sql table for invalid values and for NULL.
Valid values are: Any number and the string 'n.v.' (with and without the dots and in every possible combination as listed in my sql command)
So far, I've tried this:
select count(*)
from table1
where column1 is null
or not REGEXP_LIKE(column1, '^[0-9,nv,Nv,nV,NV,n.v,N.v,n.V,N.V]+$');
The regular expression also matches the single character values 'n','N','v','V' (with and without a following dot). This shouldn't be the case, because I only want the exact character combinations as written in the sql command to be matched. I guess the problem has to do with using REGEXP_LIKE. Any ideas?
I guess this regexp will work:
NOT REGEXP_LIKE(column1, '^([0-9]+|n\.?v\.?)$', 'i')
Note that , is not a separator, . means any character, \. means the dot character itself and 'i' flag could be used to ignore case instead of hard coding all combinations of upper and lower case characters.
No need to use regexp (performance will increase by large data) - plain old TRANSLATE is good enough for your validation.
Note that the first translate(column1,'x0123456789','x') remove all numeric charcters from the string, so if you end with nullthe string is OK.
The second translate(lower(column1),'x.','x') removes all dots from the lowered string so you expect the result nv.
To avoid cases as n.....v.... you also limit the string length.
select
column1,
case when
translate(column1,'x0123456789','x') is null or /* numeric string */
translate(lower(column1),'x.','x') = 'nv' and length(column1) <= 4 then 'OK'
end as status
from table1
COLUMN1 STATUS
--------- ------
1010101 OK
1012828n
1012828nv
n.....v....
n.V OK
Test data
create table table1 as
select '1010101' column1 from dual union all -- OK numbers
select '1012828n' from dual union all -- invalid
select '1012828nv' from dual union all -- invalid
select 'n.....v....' from dual union all -- invalid
select 'n.V' from dual; -- OK nv
You can use:
select count(*)
from table1
WHERE TRANSLATE(column1, ' 0123456789', ' ') IS NULL
OR LOWER(column1) IN ('nv', 'n.v', 'nv.', 'n.v.');
Which, for the sample data:
CREATE TABLE table1 (column1) AS
SELECT '12345' FROM DUAL UNION ALL
SELECT 'nv' FROM DUAL UNION ALL
SELECT 'NV' FROM DUAL UNION ALL
SELECT 'nV' FROM DUAL UNION ALL
SELECT 'n.V.' FROM DUAL UNION ALL
SELECT '...................n.V.....................' FROM DUAL UNION ALL
SELECT '..nV' FROM DUAL UNION ALL
SELECT 'n..V' FROM DUAL UNION ALL
SELECT 'nV..' FROM DUAL UNION ALL
SELECT 'xyz' FROM DUAL UNION ALL
SELECT '123nv' FROM DUAL;
Outputs:
COUNT(*)
5
or, if you want any quantity of . then:
select count(*)
from table1
WHERE TRANSLATE(column1, ' 0123456789', ' ') IS NULL
OR REPLACE(LOWER(column1), '.') = 'nv';
Which outputs:
COUNT(*)
9
db<>fiddle here

Extract a desired character from an alphanumeric column using concise REGEX in Oracle SQL

I am going through a "clean-up" process of a character column that holds employee rankings that should have a single character: 0-5 or M or U but instead it has up to 3 characters that are either numeric, alpha or alphanumeric.
I spent the better part of two days researching online (Regex, Stack overflow and Oracle resources) and testing and trying various options and the below "RESULT" (refer to code) is what I came up with. It does the job but I can't help but think that there is a more concise way of doing this. For example, at one point I thought I was close to accomplishing the task with a single instance of REGEXP_SUBSTR which used "|" (refer to PREV_TRY in code below). But then I couldn't figure out how to take this result and extract from it the first character for cases [1-4]{1}[ABCUL]{1} or the last character for all other cases.
Here is a reproducible example along with the solution I have so far:
WITH T AS (
SELECT 'M' EX FROM DUAL UNION ALL
SELECT 'U' FROM DUAL UNION ALL
SELECT '1A' FROM DUAL UNION ALL --some two character values are [1-4]{1}[ABCUL]{1}
SELECT TO_CHAR(ROWNUM) FROM DUAL CONNECT BY LEVEL <= 7 UNION ALL
SELECT '0' FROM DUAL UNION ALL
SELECT '113' FROM DUAL UNION ALL--if its numeric it can be 0-999
SELECT '03' FROM DUAL UNION ALL--some two character values are 0[1-4]{1}
SELECT '99' FROM DUAL UNION ALL
SELECT 'RG1' FROM DUAL UNION ALL--some three character values are RG[1-4]{1}
SELECT 'NA' FROM DUAL UNION ALL--some values are 'NA' or 'N/A'
SELECT null FROM DUAL --there are null values
)
SELECT EX
,NVL(SUBSTR(
NVL(
SUBSTR(REGEXP_SUBSTR(EX,'(^(0|RG)?[0-5MU]?$)'),-1,1)
,REGEXP_SUBSTR(EX,'^[1-4]{1}[ABCUL]{1}'))
,1,1),0) RESULT --what I came up with so far
,NVL(REGEXP_SUBSTR(EX,'(^(0|RG)?[0-5MU]?$)|(^[1-4]{1}[ABCUL]{1})'),0) PREV_TRY
FROM T
I summarize what I need to accomplish with these rules:
if its a single character then return any character that matches
[0-5MU].
if its a single digit followed by a single alpha character then
return the digit [1-4]{1}[ABCUL]{1}. E.g., '2A' returns '2'
if its RG[1-4] then return the digit. E.g., 'RG3' returns '3'
if its 0[1-4] then return the second digit. E.g., '03' returns '3'
all else return 0
You can use:
WITH T (ex)AS (
SELECT 'M' FROM DUAL UNION ALL
SELECT 'U' FROM DUAL UNION ALL
SELECT '1A' FROM DUAL UNION ALL --some two character values are [1-4]{1}[ABCUL]{1}
SELECT TO_CHAR(ROWNUM) FROM DUAL CONNECT BY LEVEL <= 7 UNION ALL
SELECT '0' FROM DUAL UNION ALL
SELECT '113' FROM DUAL UNION ALL--if its numeric it can be 0-999
SELECT '03' FROM DUAL UNION ALL--some two character values are 0[1-4]{1}
SELECT '99' FROM DUAL UNION ALL
SELECT 'RG1' FROM DUAL UNION ALL--some three character values are RG[1-4]{1}
SELECT 'NA' FROM DUAL UNION ALL--some values are 'NA' or 'N/A'
SELECT null FROM DUAL --there are null values
)
SELECT ex,
COALESCE(
REGEXP_REPLACE(
ex,
'^([0-5MU])$|^(\d)[A-Z]$|^(RG|0)([1-4])$|^.*$',
'\1\2\4'
),
'0'
) AS replacement
FROM t
Which outputs:
EX
REPLACEMENT
M
M
U
U
1A
1
1
1
2
2
3
3
4
4
5
5
6
0
7
0
0
0
113
0
3
3
99
0
RG1
1
NA
0
<null>
0
db<>fiddle here

Why is 0 an exception for to_char(0,'B9999')?

What makes the number 0 an exception to Oracle to_char(number,'B9999') mask?
In this query 0 doesn't print at all.
How can I left pad all my numbers including 0 with spaces?
Is lpad() the only alternative?
select to_char(0,'B09999') as num_fmt from dual union all
select to_char(0,'B9999') as num_fmt from dual union all
select to_char(300,'B9999') as num_fmt from dual union all
select to_char(-300,'B9999') as num_fmt from dual ;
You can use to_char(yourVal,'999') format model to get left padded blanks for integers of three digits ( Leading zeros are blank, except for a zero value ).
If you need more, than raise the number of nines upto number of digits within your integer values :
select to_char(0,'999') as num_fmt from dual union all
select to_char('00','999') from dual union all
select to_char('016','999') from dual union all
select to_char(17,'999') from dual union all
select to_char(314,'999') from dual union all
select to_char(-314,'999') from dual;
NUM_FMT
-------
0
0
16
17
314
-314
Demo

How to add a space to an existing string in Oracle character functions without using regular expressions

I have a field as name in a table with names inserted without spaces. Eg: "MarkJones".
Now I want to create a space between the first and lastname of a person within the same column to be displayed as "Mark Jones" using Oracle functions.
I have tried this query
SELECT instr('MarkJones', '%||Upper(*)||%') AS substr1,
SUBSTR('MarkJones', instr('MarkJones', '%lower(*)upper(*)%')) AS substr2,
substr1||' '||substr2
FROM dual
;
However, this query is not working. I want to try it using oracle functions including translate, substr and instr, but no regular expressions.
This approach works for the simple example given, but fails if the name has more than 2 uppercase letters in it. If this is coursework as expected, maybe the requirements are not too difficult for the names to parse as we all know that is fraught with heartache and you can never account for 100% of names from all nationalities.
Anyway my approach was to move through the string looking for uppercase letters and if found replace them with a space followed by the letter. I used the ASCII function to test their ascii value to see if they were an uppercase character. The CONNECT BY construct (needed to loop through each character of the string) returns each character in its own row so LISTAGG() was employed to reassemble back into a string and ltrim to remove the leading space.
I suspect if this is coursework it may be using some features you should not be using yet. At least you should get out of this the importance of receiving and/or giving complete specifications!
SQL> with tbl(name) as (
select 'MarkJones' from dual
)
select ltrim(listagg(case
when ascii(substr(name, level, 1)) >= 65 AND
ascii(substr(name, level, 1)) <= 90 THEN
' ' || substr(name, level, 1)
else substr(name, level, 1)
end, '')
within group (order by level)) fixed
from tbl
connect by level <= length(name);
FIXED
------------------------------------
Mark Jones
When you are ready, here's the regexp_replace version anyway :-)
Find and "remember" the 2nd occurrence of an uppercase character then replace it with a space and the "remembered" uppercase character.
SQL> with tbl(name) as (
select 'MarkJones' from dual
)
select regexp_replace(name, '([A-Z])', ' \1', 1, 2) fixed
from tbl;
FIXED
----------
Mark Jones
Not sure we should go against #Alex Poole advice, but it looks like an homework assignment.
So my idea is to point the second Upper Case. Its doable if you create a set of the upper cases, on which you valuate the position in input string iStr. Then if you're allowed to use length, you can use this position to build firstName too:
SELECT substr(iStr, 1, length(iStr)-length(substr(iStr, instr(iStr, u)))) firstName
, substr(iStr, instr(iStr, u)) lastName
, substr(iStr, 1, length(iStr)-length(substr(iStr, instr(iStr, u)))) ||' '||
substr(iStr, instr(iStr, u)) BINGO
FROM ( select 'MarkJones' iStr from dual
union all select 'SomeOtherNames' from dual -- 2 u-cases gives 2 different results
union all select 'SomeOtherOols' from dual -- only one result
union all select 'AndJim' from dual
union all select 'JohnLenon' from dual
union all select 'LemingWay' from dual
),
( select 'A' U from dual
union all select 'B' from dual
union all select 'C' from dual
union all select 'D' from dual
union all select 'E' from dual
union all select 'F' from dual
union all select 'G' from dual
union all select 'H' from dual
union all select 'I' from dual
union all select 'J' from dual
union all select 'K' from dual
union all select 'L' from dual
union all select 'M' from dual
union all select 'N' from dual
union all select 'O' from dual
union all select 'P' from dual
union all select 'Q' from dual
union all select 'R' from dual
union all select 'S' from dual
union all select 'T' from dual
union all select 'U' from dual
union all select 'V' from dual
union all select 'W' from dual
union all select 'X' from dual
union all select 'Y' from dual
union all select 'Z' from dual
) upper_cases
where instr(iStr, U) > 1
;

Filter the rows with number only data in a column SQL

I am trying to SELECT rows in a table, by applying a filter condition of identifying number only columns. It is a report only query, so we least bother the performance, as we dont have the privilege to compile a PL/SQL am unable to check by TO_NUMBER() and return if it is numeric or not.
I have to achieve it in SQL. Also the column is having the values like this, which have to be treated as Numbers.
-1.0
-0.1
-.1
+1,2034.89
+00000
1023
After ground breaking research, I wrote this.(Hard time)
WITH dummy_data AS
( SELECT '-1.0' AS txt FROM dual
UNION ALL
SELECT '+0.1' FROM dual
UNION ALL
SELECT '-.1' FROM dual
UNION ALL
SELECT '+1,2034.89.00' FROM dual
UNION ALL
SELECT '+1,2034.89' FROM dual
UNION ALL
SELECT 'Deva +21' FROM dual
UNION ALL
SELECT '1+1' FROM dual
UNION ALL
SELECT '1023' FROM dual
)
SELECT dummy_data.*,
REGEXP_COUNT(txt,'.')
FROM dummy_data
WHERE REGEXP_LIKE (TRANSLATE(TRIM(txt),'+,-.','0000'),'^[-+]*[[:digit:]]');
I got this.
TXT REGEXP_COUNT(TXT,'.')
------------- ---------------------
-1.0 4
+0.1 4
-.1 3
+1,2034.89.00 13 /* Should not be returned */
+1,2034.89 10
1+1 3 /* Should not be returned */
1023 4
7 rows selected.
Now terribly confused with 2 Questions.
1) I get +1,2034.89.00 too in result, I should eliminate it. (means, two decimal points) Not just decimal point, double in every other special character (-+,) should be eliminated)
2) To make it uglier, planned to do a REGEXP_COUNT('.') <= 1. But it is not returning my expectation, while selecting it, I see strange values returned.
Can someone help me to frame the REGEXP for the avoiding the double occurences of ('.','+','-')
The following expression works for everything, except the commas:
'^[-+]*[0-9,]*[.]*[0-9]+$'
You can check for bad comma placement with additional checks like:
not regexp_like(txt, '[-+]*,$') and not regexp_like(txt, [',,'])
First you remove plus and minus with translate and then you wonder why their position is not considered? :-)
This should work:
WITH dummy_data AS
( SELECT '-1.0' AS txt FROM dual
UNION ALL
SELECT '+0.1' FROM dual
UNION ALL
SELECT '-.1' FROM dual
UNION ALL
SELECT '+12034.89.00' FROM dual -- invalid: duplicate decimal separator
UNION ALL
SELECT '+1,2034.89' FROM dual -- invalid: thousand separator placement
UNION ALL
SELECT 'Deva +21' FROM dual -- invalid: letters
UNION ALL
SELECT '1+1' FROM dual -- invalid: plus sign placement
UNION ALL
SELECT '1023' FROM dual
UNION ALL
SELECT '1.023,88' FROM dual -- invalid: decimal/thousand separators mixed up
UNION ALL
SELECT '1,234' FROM dual
UNION ALL
SELECT '+1,234.56' FROM dual
UNION ALL
SELECT '-123' FROM dual
UNION ALL
SELECT '+123,0000' FROM dual -- invalid: thousand separator placement
UNION ALL
SELECT '+234.' FROM dual -- invalid: decimal separator not followed by digits
UNION ALL
SELECT '12345,678' FROM dual -- invalid: missing thousand separator
UNION ALL
SELECT '+' FROM dual -- invalid: digits missing
UNION ALL
SELECT '.' FROM dual -- invalid: digits missing
)
select * from dummy_data
where regexp_like(txt, '[[:digit:]]') and
(
regexp_like(txt, '^[-+]{0,1}([[:digit:]]){0,3}(\,([[:digit:]]){0,3})*(\.[[:digit:]]+){0,1}$')
or
regexp_like(txt, '^[-+]{0,1}[[:digit:]]*(\.[[:digit:]]+){0,1}$')
);
You see, you need three regular expressions; one to guarantee that there is at least one digit in the string, one for numbers with thousand separators, and one for numbers without.
With thousand separators: txt may start with one plus or minus sign, then there may be up to three digits. These may be followed by a thousand separator plus three digits several times. Then there may be a decimal separator with at least one following number.
Without thousand separators: txt may start with one plus or minus sign, then there may be digits. Then there may be a decimal separator with at least one following number.
I hope I haven't overlooked anything.
I just tried to correct the mistakes of you and made the SQL simple as possible. But not neat!
WITH dummy_data AS
( SELECT '-1.0' AS txt FROM dual
UNION ALL
SELECT '+.0' FROM dual
UNION ALL
SELECT '-.1' FROM dual
UNION ALL
SELECT '+1,2034.89.0' FROM dual
UNION ALL
SELECT '+1,2034.89' FROM dual
UNION ALL
SELECT 'Deva +21' FROM dual
UNION ALL
SELECT 'DeVA 234 Deva' FROM dual
UNION ALL
SELECT '1023' FROM dual
)
SELECT to_number(REPLACE(txt,',')),
REGEXP_COUNT(txt,'.')
FROM dummy_data
WHERE REGEXP_LIKE (txt,'^[-+]*')
AND NOT REGEXP_LIKE (TRANSLATE(txt,'+,-.','0000'),'[^[:digit:]]')
AND REGEXP_COUNT(txt,',') <= 1
AND REGEXP_COUNT(txt,'\+') <= 1
AND REGEXP_COUNT(txt,'\-') <= 1
AND REGEXP_COUNT(txt,'\.') <= 1;