Oracle : SQL using REGEXP_LIKE - sql

I need to extract string from a value of a cell in Oracle table. How can I do that?
VALUE
--------
/var1/var2/var3/C751994ZP1QT11/var4.itp
I want to extract "C751994ZP1QT11" from the value using SQL. I searched some function like REGEXP_LIKE, but I am not quite understand how to use it.
Thanks ahead for the help.

If you want to extract a string, you need to use regexp_substr(). regexp_like() would return a boolean indicating if a certain regular expression matches your string, while regexp_substr() actually returns the matched string.
In your case, assuming you want the string between /** and **/, you could use something like this:
select
regexp_substr(
'/var1/var2/var3/**C751994ZP1QT11**/var4.itp',
'/\*\*.*\*\*/')
from dual
Refer here for some useful shortcuts on regular expressions with Oracle.

select REGEXP_REPLACE('/var1/var2/var3/**C751994ZP1QT11**/var4.itp','.*\*\*(.*)\*\*.*','\1') result from dual

Related

Query to take the value after '/'

Suppose there is a value 842545/003. I need to take the part after '/'.
84454/02. I want a query to take the only 02 from here
You can try below substr() and instr() function
select substr('84454/02',instr('84454/02','/')+1,
length('84454/02')-instr('84454/02','/')) as val
from dual
You can use a regex (with the usual warnings about regex performance - the simple string functions like instr and substr are faster if you are processing millions of rows).
regexp_replace(yourcolumn, '^.*/')
This removes everything up to and including the / character (or the final one if there is more than one).
If your are using SQL, you can use this.
SELECT SUBSTRING('84454/02',PATINDEX('%/%', '84454/02')+1,LEN('84454/02'));
'84454/02'= Column name

Regular expression to return number after matched string in oracle

I have a query:
select ITEM_ID from system_items where id=4020;
I want a regular expression that takes the above query as input and matches for pattern "id=" and returns 4020.
Please let me know if you have any suggestions, as I have been trying with REGEXP_SUBSTR in Oracle and couldn't get it.
REGEX_SUBSTR won't allow a look-behind like (?<=id=\s*)\d+ so I suspect you need to do this in two operations. First get id=4020, then strip the id=.
One possible way of doing that would be:
REGEXP_SUBSTR(REGEXP_SUBSTR(a, 'id=\s*\d+'), '\d+')
SQLFiddle
This should do it
/id=(\d+)/
id is literal match
() are used for making the capture groups
\d is more numbers
+ ensures 1 or more
demo here http://rubular.com/r/GBxfhID5hS

How to remove part of the string in oracle

Input data:
abcdef_fhj_viji.dvc
Expected output:
fhj_viji.dvc
The part to be trimmed is not constant.
Use the REPLACE method
Select REPLACE('abcdef_fhj_viji.dvc','abcde','')
If you want this query for your table :
Select REPLACE(column,'abcde','') from myTable
For update :
UPDATE TABLE
SET column = REPLACE(column,'abcde','')
select substr('abcdef_fhj_viji.dvc',instr('abcdef_fhj_viji.dvc','_')+1) from dual
So, Its all depends on INSTR function, define from which position and which occurrence, you will get the index and pass that index to SUBSTR to get your string.
Since you didn't give a lot of information I'm gonna assume some.
Let's assume you want a prefix of some string to be deleted. A good way to do that is by using Regular Expressions. There's a function called regexp_replace, that can find a substring of a string, depending on a pattern, and replace it with a different string. In PL/SQL you could write yourself a function using regexp_replace, like this:
function deletePrefix(stringName in varchar2) return varchar2 is
begin
return regexp_replace(stringName, '^[a-zA-Z]+_', '');
end;
or just use this in plain sql like:
regexp_replace(stringName, '^[a-zA-Z]+_', '');
stringName being the string you want to process, and the ^[a-zA-Z]+_ part depending on what characters the prefix includes. Here I only included upper- and lowercase letters.

Select query that displays Joined words separately, not using a function

I require a select query that adds a space to the data based on the placement of the capital letters i.e. 'HelpMe' using this query would be displayed as 'Help Me' . Note i cannot use a stored function to do this the it must be done in the query itself. The Data is of variable length and query must be in SQL. Any Help will be appreciated.
Thanks
You need to use user defined function for this until MS give us support for regular expressions. Solution would be something like:
SELECT col1, dbo.RegExReplace(col1, '([A-Z])',' \1') FROM Table
Aldo this would produce leading space that you can remove with TRIM.
Replace regular expresion function:
http://connect.microsoft.com/SQLServer/feedback/details/378520
About dbo.RegexReplace you can read at:
TSQL Replace all non a-z/A-Z characters with an empty string
Assume if you are using Oracle RDBMS, you use the following,
REGEX_REPLACE
SELECT REGEXP_REPLACE('ILikeToWatchCSIMiami',
'([A-Z.])', ' \1')
AS RX_REPLACE
FROM dual
;
Managed to get this output: * SQLFIDDLE
But as you see it doesn't treat well on words such as CSI though.

How to extract group from regular expression in Oracle?

I got this query and want to extract the value between the brackets.
select de_desc, regexp_substr(de_desc, '\[(.+)\]', 1)
from DATABASE
where col_name like '[%]';
It however gives me the value with the brackets such as "[TEST]". I just want "TEST". How do I modify the query to get it?
The third parameter of the REGEXP_SUBSTR function indicates the position in the target string (de_desc in your example) where you want to start searching. Assuming a match is found in the given portion of the string, it doesn't affect what is returned.
In Oracle 11g, there is a sixth parameter to the function, that I think is what you are trying to use, which indicates the capture group that you want returned. An example of proper use would be:
SELECT regexp_substr('abc[def]ghi', '\[(.+)\]', 1,1,NULL,1) from dual;
Where the last parameter 1 indicate the number of the capture group you want returned. Here is a link to the documentation that describes the parameter.
10g does not appear to have this option, but in your case you can achieve the same result with:
select substr( match, 2, length(match)-2 ) from (
SELECT regexp_substr('abc[def]ghi', '\[(.+)\]') match FROM dual
);
since you know that a match will have exactly one excess character at the beginning and end. (Alternatively, you could use RTRIM and LTRIM to remove brackets from both ends of the result.)
You need to do a replace and use a regex pattern that matches the whole string.
select regexp_replace(de_desc, '.*\[(.+)\].*', '\1') from DATABASE;