using Substr and instr in SQL - 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.

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

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

how to split the value in oracle

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.

Display 9999999999 as 9-999-99999-9 in SQL

I am trying to display a column that is in the VARCHAR format of 9999999999 as 9-999-99999-9.
I know that TO_CHAR and TO_NUMBER wont work and I know I'm suppose to use SUBSTR to do this.
I feel like I could get this done if I was able to use multiple SUBSTR functions together as one column like:
SELECT SUBSTR(column, 0, 1) - SUBSTR(column, 1, 3) etc. but that wont work either.
Any guidance is MUCH appreciated.
You could use a regular expression search and replace
select regexp_replace('9999999999','(.)(...)(.....)(.)','\1-\2-\3-\4') from dual;
You should be able to use a combination of the SUBSTR function and the Oracle string concatenation operator, ||.
SELECT SUBSTR(column, 1, 1)
|| '-'
|| SUBSTR(column, 2, 3)
|| '-'
|| SUBSTR(column, 4, 5)
|| '-'
|| SUBSTR(column, 9, 1)
I think that using to_char is actually the best option. It just requires a judicious use of format models.
This should do it, but allow you to extend extremely easily without having to create a new query each time.
select replace(to_char( 999999999,'9,99,99999,9'),',','-') from dual
I've created a little SQL Fiddle to demonstrate.
Just another way you can do it, by fiddling with the NLS numeric character setting:
select to_char(9999999999, 'fm9g999g99999g9', 'NLS_NUMERIC_CHARACTERS=''.-''')
from dual;
9-999-99999-9
SELECT SUBSTR(9999999999,1,1) ||
'-' ||
SUBSTR(9999999999,2,3) ||
'-' ||
SUBSTR(9999999999,4,5) ||
'-' ||
SUBSTR(9999999999,10,1) FROM DUAL;
Suppose there is Phonenumber field in address table:
The query to display the date in (999)999-9999 format is as follows:
select concat(concat('(',substr(phone,1,3),')'),substr(phone,4,3),'-',substr(phone,7,4))
as Phone from address;