In code_subject there are values in the following formats string1#string2#integer#string3. How to extract integer? - sql

In the table code_subject there is a row with a following format string1#string2#integer#string3. How to extract a number (integer) from it?
IS IT LIKE THAT?:
SELECT REGEXP_SUBSTR('integer') "REGEXPR_SUBSTR" FROM code_subject

One way could be using regexp_replace:
select regexp_replace('stringA#stringB#1234#stringC', '[^0-9]', '')
from dual;
Another possibility is:
select substr('string1#string2#1234#string3', instr('string1#string2#1234#string3', '#', 1, 2) + 1,
instr('string1#string2#1234#string3', '#', 1, 3) - instr('string1#string2#1234#string3', '#', 1, 2) - 1)
from dual;

Related

Regex values after special character with empty values

I am struggle with regex to split spring into columns in Oracle database.
select (REGEXP_SUBSTR(replace('1:::9999', ' ',''), '[^: ]+', 1, 4)) from dual;
I need to obtain 4th value from that string as a column value, sometimes values at position 2,3 are empty and my query doesn't work. I am trying to figure out what regex will work
You can use
select (REGEXP_SUBSTR(replace('1:::9999', ' ',''), '([^: ]*)(:|$)', 1, 4, 'i', 1)) from dual;
Here, the ([^: ]*)(:|$) matches
([^: ]*) - Group 1: any zero or more chars other than : and space
(:|$) - Group 2, either : or end of string.
You do not need a (slower) regex for this task, use simple substr/instr functions:
with input_(val) as (
select '1:::9999' from dual
union all
select '1:2::' from dual
union all
select '1:2::3:5' from dual
)
, replaced as (
select input_.*, replace(val, ' ', '') as val_replaced
from input_
)
select
val,
substr(
val_replaced,
/*Locate the first occurrence of a colon and get a substring ...*/
instr(val_replaced, ':', 1, 3) + 1,
/*.. until the end, if the next colon is absent, or until the next colon*/
nvl(nullif(instr(val_replaced, ':', 1, 4), 0), length(val_replaced) + 1) - instr(val_replaced, ':', 1, 3) - 1
) as col
from replaced
VAL
COL
1:::9999
9999
1:2::
null
1:2::3:5
3
fiddle with performance difference.

selected splitted value in oracle

I have two columns A, B in oracle where A value has values like that xx-target-xx
xx any data but target is exists
A
--------
xx-target-xx
xx-target
i neet to return only 'target' from text
i tired this
select TRIM(substr(A, 0, instr(A, '-') - 1)) from mytable
but the result returns xx not target
Use REGEXP_SUBSTR. You want the second string of any characters except the minus sign:
select a, regexp_substr(a, '[^-]+', 1, 2) from mytable;
Using INSTR and SUBSTR instead is a tad more complicated, but possible of course:
select a, substr(a,
instr(a, '-') + 1,
instr(a || '-', '-', 1, 2) - instr(a, '-') - 1
) as value
from mytable;
Demo: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=e75b878bbd6300e9207cd698bb3029ec

How do I get substring after a character when the occurance of the character keeps changing

Example
123\.456.578.910.ABC
123\.456.578.910
Expected result
123\.456.578
123\.456.578
For the both the inputs I should get only the first 3
I tried the regexp and substring and instr but I’m not getting the results
We can use REGEXP_SUBSTR here with a capture group:
SELECT REGEXP_SUBSTR(col, '^(\d+(\.\d+)*)', 1, 1, NULL, 1)
FROM yourTable;
Demo
Traditional, substr + instr combination is another option:
Sample data:
SQL> with test (col) as
2 (select '123\.456.578.910.ABC' from dual union all
3 select '123\.456.578.910' from dual
4 )
Query begins here:
5 select col,
6 substr(col, 1, instr(col, '.', 1, 3) - 1) result
7 from test;
COL RESULT
-------------------- --------------------
123\.456.578.910.ABC 123\.456.578
123\.456.578.910 123\.456.578
SQL>
If you value will always have at least 3 . characters then you can use:
SELECT value,
SUBSTR(value, 1, INSTR(value, '.', 1, 3) - 1) AS expected
FROM table_name;
If it may have fewer and you want the entire string in those cases then:
SELECT value,
CASE INSTR(value, '.', 1, 3)
WHEN 0
THEN value
ELSE SUBSTR(value, 1, INSTR(value, '.', 1, 3) - 1)
END AS expected
FROM table_name;
Which, for your sample data:
CREATE TABLE table_name (value) AS
SELECT '123\.456.578.910.ABC' FROM DUAL UNION ALL
SELECT '123\.456.578.910' FROM DUAL;
Both outputs:
VALUE
EXPECTED
123.456.578.910.ABC
123.456.578
123.456.578.910
123.456.578
db<>fiddle here

How to get specific value from specific position by substr-instr

I have some filename like 'abc-type-bank-20200112-1578796204118.csv'
i want to get the value 'bank' from above which is 3rd value if I cut the string by '-'.
If my filename is like abc-type-XYZ-20200112-1578796204118.csv, I should get 'XYZ' from 3rd position.
I was trying to get by below code, but it giving wrong value.
select substr(v_FILE_NAME,1,instr(v_FILE_NAME, '-') + 3) as name from (
select 'abc-type-bank-20200112-1578796204118.csv' as v_FILE_NAME from dual);
To use SUBSTR and INSTR to extract your desired substring you'd do something like
WITH cteData AS (SELECT 'abc-type-bank-20200112-1578796204118.csv' AS FILENAME FROM DUAL),
ctePos AS (SELECT FILENAME,
INSTR(FILENAME, '-', 1, 2) AS DASH_2_POS,
INSTR(FILENAME, '-', 1, 3) AS DASH_3_POS
FROM cteData)
SELECT SUBSTR(FILENAME, DASH_2_POS + 1, DASH_3_POS - DASH_2_POS - 1) AS SUBSTR_3
FROM ctePos
The above will extract the substring 'bank' from the sample data.
db<>fiddle here
The use of dual suggests Oracle. In that database you can use regexp_substr():
select regexp_substr(filename, '[^-]+', 1, 3)
from (select 'abc-type-XYZ-20200112-1578796204118.csv' as filename from dual) x;

Split a string based on hyphen

I have a string like this
XX0099-X01
I would like to split the string into two based on the hyphen, i.e.
XX0099 and X01
I have tried as
SELECT 'XX0099-X01',
SUBSTR('XX0099-X01',
1,
INSTR('XX0099-X01',
'-',
1
) -1
)
FROM dual
Not sure how to get the second part.
We have an old legacy system which still using 8i Oracle database.
Using combination of substr and instr:
select
substr(s, 1, instr(s, '-') - 1),
substr(s, instr(s, '-') + 1)
from t;
Another way is using regexp_substr if supported:
select
regexp_substr('XX0099-X01','[^-]+', 1, 1),
regexp_substr('XX0099-X01','[^-]+', 1, 2)
from dual;
If you can't use a regex substring/replacement, here is an option which uses Oracle's base string functions:
SELECT SUBSTR(col, 1, INSTR(col, '-') - 1) AS first_part,
SUBSTR(col, INSTR(col, '-') + 1) AS second_part
FROM yourTable