how to split the value in oracle - sql

I have worked with MySQL and new to oracle. In MySQL we have a function SUBSTRING_INDEX(), I want to replace it in oracle, Any help plz
SQL>select SUBSTRING_INDEX('JD;EQ;0001', ';', -1) from dual;
Result:
0001
I want a same result in oracle. Is there any function in oracle that return the same result in oracle?
I have tried but no expected result.
SELECT substr('CLUBORACLE',3,2) RES FROM dual;

SELECT SUBSTR('JD;EQ;0001', INSTR('JD;EQ;0001', ';', -1) + 1) FROM dual
In the query above, INSTR('JD;EQ;0001', ';', -1) would return 6, which is the position of the last semicolon in the expression. You want to take the substring from the position after the last semicolon until the end of the string.
Look here for a good SO question about Oracle's INSTR.

You can also use regex: SELECT REGEXP_SUBSTR('JD;EQ;0001', '[^;]+', 1, your_occurance_number) FROM dual; but SUBSTR+INSTR should be faster.

Related

SUBSTR in sql oracle from right

I have data in table 000_ABC_AXEL. The expectation is that i have to exclude data after the last '_' and get 000_ABC in oracle sql? Any suggestions?
Need sql query to achieve below
for ex:
a_222_4 -- > expected result :a_222
123_xyz_0 -- >expected result :123_xyz
A regex replacement fits your requirement nicely:
SELECT col, REGEXP_REPLACE(col, '_[^_]+$', '') AS col_out
FROM yourTable;
You can do it with simple string functions (which are much faster than regular expressions) by finding the sub-string up to the character before the last underscore:
SELECT SUBSTR(col, 1, INSTR(col, '_', -1) - 1) AS first_parts
FROM table_name;
Which, for the sample data:
CREATE TABLE table_name (col) AS
SELECT 'a_222_4' FROM DUAL UNION ALL
SELECT '123_xyz_0' FROM DUAL;
Outputs:
FIRST_PARTS
a_222
123_xyz
fiddle

Convert regexp_substr statement to SQL Server equivalent

I am new to converting oracle sql statement to t-sql. Can you please help me convert the following statement?
select regexp_substr(TO_CHAR(X.ITEM), '[^|]+', 1, level) CONCAT
from dual
connect by regexp_substr(TO_CHAR(X.ITEM), '[^|]+', 1, level) is not null
It looks like you want to split a delimited list into rows, using character | as separator.
If you are running SQL Server 2016 or higher, you can use string_split() for this. Assuming that x is the table where item is stored, you would go:
select x.*, value
from x
cross apply string_split(item, '|')

how to extract the part before "/" character by oracle regexp_substr()

I want to extract the part before the 1st /, how can I do that?
SELECT REGEXP_SUBSTR('prAct1-03/cPMI-01/iPEPC9-01/t35s-08','(pr.*)/',1,1) as promoter
FROM dual;
did not work, and seems oracle did not support Lookahead and Lookbehind.
Thanks!
I would just use the base string functions here:
SELECT SUBSTR(col, 1, INSTR(col, '/') - 1) AS first_part
FROM yourTable;
Demo
If you really wanted to use regex, then try:
SELECT REGXP_SUBSTR(col, '^[^/]+')
FROM yourTable;
One option uses REGEXP_REPLACE() function :
SELECT REGEXP_REPLACE('prAct1-03/cPMI-01/iPEPC9-01/t35s-08','(\S)(/.*)','\1') AS promoter
FROM dual;
Demo
Please use below query,
select substr('prAct1-03/cPMI-01/iPEPC9-01/t35s-08', 0, instr('prAct1-03/cPMI-01/iPEPC9-01/t35s-08', '/')-1) as promoter from dual;

ORACLE:SQL REGEXP_SUBSTR that returns the column value after last backslash(/)

ORACLE:SQL REGEXP_SUBSTR that returns the column value after last backslash(/)
example:
https://test/test/test/test/getTest/1234
expected value: 1234
You don't need regular expressions for this. You can simply using substr and instr which are likely to perform faster:
select
substr(col, instr(col, '/', -1) + 1)
from t;
Demo
If you must use regexp_substr (for some reason) then use:
select regexp_substr(col, '[^/]+$') from t;
Demo
If you need with REGEXP_SUBSTR also, then:
SELECT REGEXP_SUBSTR ('https://test/test/test/test/getTest/1234' , '[^/]+$' ) from dual

using Substr and instr in SQL

I am trying to extract the middle of the string from record. I want to get the only middle of the string.
select instr('WUK00000105376:WUKE03960761:WUKR0093868603',':')
from dual;
I want to get only WUKE03960761 from this string.
I have tried by using sustring and instring to get the output but i am not getting.
SELECT SUBSTR(col,
INSTR(col, ':') + 1,
INSTR(col, ':', 1, 2) - INSTR(col, ':') - 1)
FROM dual
Another option is to use regexp_substring() to get the string between the two colons:
select regexp_substr('WUK00000105376:WUKE03960761:WUKR0093868603',':[A-Z0-9]+:')
from dual;
This however would also return the colons, but you can also tell regexp_substr() to return a specific group from the regex:
select regexp_substr('WUK00000105376:WUKE03960761:WUKR0093868603',':([A-Z0-9]+):',1,1,'i',1)
from dual;
But Tim's solution using substr and instr is most likely going to be a lot faster.