Need to remove the exact group of characters - sql

I need to remove all the characters after a particular string (-->).
select
REGEXP_SUBSTR('-->Team Name - Red-->blue', '[^(-->)]+')
from dual;
expected result from the above query is "Team Name - Red". But its returning "Team Name".
Its filtering out everything whenever it matches any of one character.

You can still use Regexp_Substr() analytic function :
Select Regexp_Substr('-->Team Name - Red-->blue',
'-{2}>(.*?)-{2}>',1,1,null,1) as "Result"
From dual;
Result
---------------
Team Name - Red
-{2}> ~ exactly twice occurence of - and single occurence of > e.g. ( --> )
(.*?) ~ matches anything delimited by the pattern above
Demo

You could try using REGEXP_REPLACE here with a capture group:
SELECT
REGEXP_REPLACE('-->Team Name - Red-->blue', '.*-->(.*?)-->.*', '\1')
FROM dual;
The output from this is Team Name - Red
Demo

It seems that you, actually, want to return string between two --> marks. A good, old substr + instr option would be
SQL> with test (col) as
2 (select '-->Team Name - Red-->blue' from dual)
3 select substr(col,
4 instr(col, '-->', 1, 1) + 3,
5 instr(col, '-->', 1, 2) - instr(col, '-->', 1, 1) - 3
6 ) result
7 from test;
RESULT
---------------
Team Name - Red
SQL>

Related

shorten the string to the desired values

I have a table with a "Link" attribute. It has the following meaning:
INC102
INC1020
INC10200
I want to get the following result:
INC102
INC1020
INC10200
I need to leave the INC and the numbers after it without .
Tell me which command will help here? Since I understand that "Substr" will not work here.
I am use SQL Developer - Oracle
If you just want to extract "INC" with the following digits, use regexp_substr():
select regexp_substr(link, 'INC[0-9]+')
Here is a db<>fiddle.
Since I understand that "Substr" will not work here.
Says who?
SQL> with test (col) as
2 (select 'INC102' from dual union all
3 select 'INC1020' from dual union all
4 select 'INC10200' from dual
5 )
6 select
7 substr(col,
8 instr(col, '>') + 1,
9 instr(col, '<', instr(col, '>')) - instr(col, '>') - 1
10 ) result
11 from test;
RESULT
------------------------------------------------------------------------------------------------------------------------
INC102
INC1020
INC10200
SQL>
What does it do?
lines #1 - 5: sample data
line #8: starting point of the SUBSTR function is one character after the first > sign
line #9: length (used as the 3rd parameter of the SUBSTR) is position of the first < that follows the first > minus position of the first >
And that's it ... why wouldn't it work?
You could treat [<>] as the word delimiter and take the second word:
with test (col) as
( select 'INC102' from dual union all
select 'INC1020' from dual union all
select 'INC10200' from dual
)
select regexp_substr(col,'[^<>]+', 1, 2)
from test;
REGEXP_SUBSTR(COL,'[^<>]+',1,2)
-------------------------------
INC102
INC1020
INC10200

SQL and Oracle query to extract every thing before last two periods

I need to extract every thing before last two periods
eg.
Input: AA.BBB.12.11.cc
Output: AA.BBB.12
Following is the sample query I am using but that returns only the characters before first period, but that is not I needed.
SELECT REGEXP_SUBSTR(t.column,'[^.]+',1,1)
AS output
FROM MY_Table t where t.column is not null and rownum=1
I would use REGEXP_REPLACE here:
SELECT REGEXP_REPLACE(t.column, '\.[^.]+\.[^.]+$', '')
FROM MY_table
WHERE t.column IS NOT NULL AND rownum = 1;
The regex pattern \.[^.]+\.[^.]+$ will match starting with the second to last dot, all content until the end (including also the last dot).
You can simply use INSTR and SUBSTR as following:
SQL> SELECT
2 SUBSTR('AA.BBB.12.11.ccCC', 1, INSTR('AA.BBB.12.11.ccCC', '.', -2, 2) - 1) AS RESULT
3 FROM DUAL;
RESULT
---------
AA.BBB.12
SQL>
-- Update --
For the question asked in the comment, use the following query:
SQL> SELECT
2 SUBSTR('AA.BB.CC.DD', 1, INSTR('AA.BB.CC.DD', '.', 1, 3) - 1) AS RESULT
3 FROM DUAL;
RESULT
--------
AA.BB.CC
SQL>
Cheers!!

Oracle REGEXP_SUBSTR | Fetch string between two delimiters

I have a string Organization, INC..Truck/Equipment Failure |C. I want to fetch the sub-string after organization name (after two '..' characters) and before pipe character. So the output string should be - Truck/Equipment Failure.
Can you please help.
I have been trying forming regexp like this but doesn't seem working.
select regexp_substr('Organization, INC..Truck/Equipment Failure |C', '[^.]+',1,2) from dual;
You may use this.
SELECT REGEXP_SUBSTR ('Organization, INC..Truck/Equipment Failure |C',
'([^.]+)\|',
1,
1,
NULL,
1)
FROM DUAL;
EDIT: This will match exactly two dots followed by one or more characters other than a | till the end of string.
SELECT REGEXP_SUBSTR ('Organization, INC..Truck/Equipment Failure',
'\.{2}([^|]+)',
1,
1,
NULL,
1)
FROM DUAL;
DEMO
Classic SUBSTR + INSTR option:
SQL> with test as (select 'Organization, INC..Truck/Equipment Failure |C' col from dual)
2 select substr(col, instr(col, '..') + 2,
3 instr(col, '|') - instr(col, '..') - 2
4 ) result
5 from test;
RESULT
------------------------
Truck/Equipment Failure
SQL>

How to extract the number from a string using Oracle?

I have a string as follows: first, last (123456) the expected result should be 123456. Could someone help me in which direction should I proceed using Oracle?
It will depend on the actual pattern you care about (I assume "first" and "last" aren't literal hard-coded strings), but you will probably want to use regexp_substr.
For example, this matches anything between two brackets (which will work for your example), but you might need more sophisticated criteria if your actual examples have multiple brackets or something.
SELECT regexp_substr(COLUMN_NAME, '\(([^\)]*)\)', 1, 1, 'i', 1)
FROM TABLE_NAME
Your question is ambiguous and needs clarification. Based on your comment it appears you want to select the six digits after the left bracket. You can use the Oracle instr function to find the position of a character in a string, and then feed that into the substr to select your text.
select substr(mycol, instr(mycol, '(') + 1, 6) from mytable
Or if there are a varying number of digits between the brackets:
select substr(mycol, instr(mycol, '(') + 1, instr(mycol, ')') - instr(mycol, '(') - 1) from mytable
Find the last ( and get the sub-string after without the trailing ) and convert that to a number:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE test ( str ) AS
SELECT 'first, last (123456)' FROM DUAL UNION ALL
SELECT 'john, doe (jr) (987654321)' FROM DUAL;
Query 1:
SELECT TO_NUMBER(
TRIM(
TRAILING ')' FROM
SUBSTR(
str,
INSTR( str, '(', -1 ) + 1
)
)
) AS value
FROM test
Results:
| VALUE |
|-----------|
| 123456 |
| 987654321 |

Oracle sql : get only specific part of a substring

I'm struggling with a query in Oracle SQL, wanting to get some timings out of some text stored in an Oracle db.
Table :
kde_test (myString varchar(50))
Table contents (3 records):
'task1 - 6m'
'task2 - 66m'
'task3 - 666m'
I would like to get only the interesting part of the string, being the timings, so I would like to get only '6', '66' & '666' as results.
Searched this forum a bit, and got up with this query eventually, but it seems I do not completely get it, as the results it returns are :
6m
66m
666m
select
CASE
WHEN myString like 'task1%' THEN substr(myString,9,INSTR(myString,'m',1,1)-1)
WHEN myString like 'task2%' THEN substr(myString,9,INSTR(myString,'m',1,1)-1)
WHEN myString like 'task3%' THEN substr(myString,9,INSTR(myString,'m',1,1)-1)
END
from kde_test
where myString like 'task%'
EDIT :
Since some solutions (thanks already for quick response) take into account the specific values (eg. all 3 records ending on '6m'), maybe it best to take into account the values could be :
Table contents (3 records):
'task1 - 6m'
'task2 - 58m'
'task3 - 123m'
you can use this way too
select regexp_replace(SUBSTR('task3 - 666m' ,
INSTR('task3 - 666m', '-',1, 1)+1, length('task3 - 666m')), '[A-Za-z]')
from dual
result :666
Use SUBSTR and INSTR and make it dynamic.
SUBSTR(str,
instr(str, ' - ', 1, 1) +3,
instr(str, 'm', 1, 1) -
instr(str, ' - ', 1, 1) -3
)
For example,
SQL> WITH DATA AS(
2 SELECT 'task1 - 6m' str FROM dual UNION ALL
3 SELECT 'task2 - 66m' str FROM dual UNION ALL
4 SELECT 'task3 - 666m' str FROM dual UNION ALL
5 SELECT 'task4 - 58m' str FROM dual UNION ALL
6 SELECT 'task5 - 123m' str FROM dual
7 )
8 SELECT str,
9 SUBSTR(str, instr(str, ' - ', 1, 1) +3,
10 instr(str, 'm', 1, 1) - instr(str, ' - ', 1, 1) -3) new_st
11 FROM DATA;
STR NEW_STR
------------ ------------
task1 - 6m 6
task2 - 66m 66
task3 - 666m 666
task4 - 58m 58
task5 - 123m 123
SQL>
You can use the regex_substr function. \d+ means one or more digits, and $ anchors the end of the string.
select regexp_substr(str, '\d+m$')
from mytable
Example at SQL Fiddle.
In order to correct your current query, you should change the following string - "INSTR(myString,'m',1,1)-1" to "INSTR(myString,'m',1,1)-9".
However, the other answers provided above seem like a more elegant solution to your problem.
I did feel the need to publish this just to clarify what wasn't working well in current query - in INSTR function returns the position of the m letter, and then used as the length of the string to print. What my fix does is telling the query to print everything from the 9th character until the position of the m letter, which results in the task time required.
I have tried to divide this into two parts
First pick the string after -
regexp_substr ('task1 - 1234m', '[^ _ ]+',1, 3) --results 1234m
Fetch the number part of the string fetched from output of first
regexp_substr(regexp_substr ('task1 - 1234m', '[^ _ ]+',1, 3),'[[:digit:]]*')
--output 1234
So,the final query is
SELECT regexp_substr(regexp_substr (mystring, '[^ _ ]+',1, 3),'[[:digit:]]*')
FROM kde_test;
Use This:-
select substr(replace(myString,'m',''),9) output
from kde_test
where myString like 'task%'