Regular Expression to match both first characters and last character in oracle - oracle

I have a table with a column with the structure:
Table name : re_result
res_id
--------------
PSI8765450
PSIRRRRTY781
ABCD000001
I want to fetch the values starting with PSI and ending with 1. My expected output is PSIRRRRTY781.
I am using query
Select * from re_result
Where regexp_like(^PSI*1)
But I am not getting the output. I am getting both PSIRRRRTY781 and ABCD000001.
Plz help

You do not need regular expressions; a simpler LIKE may do the work:
select res_id
from re_result
where res_id like 'PSI%1'
The same thing can be done with regexp:
where regexp_like(res_id, '^PSI(.*)1$')
This matches 'PSI' in the beginning of the string and '1' as last character, just before the end of string ($).
Here you find something more on regexp in Oracle

Another way to handle your query.
SELECT res_id FROM re_result WHERE UPPER(res_id) like UPPER('PSI%1')

Related

Oracle SQL: Using Replace function while Inserting

I have a query like this:
INSERT INTO TAB_AUTOCRCMTREQUESTS
(RequestOrigin, RequestKey, CommentText) VALUES ('Tracker', 'OPM03865_0', '[Orange.Security.OrangePrincipal]
em[u02650791]okok
it's friday!')
As expected it is throwing an error of missing comma, due to this it's friday! which has a single quote.
I want to remove this single quote while inserting using Replace function.
How can this be done?
Reason for error is because of the single Quote. In order to correct it, you shall not remove the single quote instead you need to add one more i.e. you need to make it's friday to it''s friday while inserting.
If you need to replace it for sure, then try the below code :
insert into Blagh values(REPLACE('it''s friday', '''', ''),12);
I would suggest using Oracle q quote.
Example:
INSERT INTO TAB_AUTOCRCMTREQUESTS (RequestOrigin, RequestKey, CommentText)
VALUES ('Tracker', 'OPM03865_0',
q'{[Orange.Security.OrangePrincipal] em[u02650791]okok it's friday!}')
You can read about q quote here.
To shorten this article you will follow this format: q'{your string here}' where "{" represents the starting delimiter, and "}" represents the ending delimiter. Oracle automatically recognizes "paired" delimiters, such as [], {}, (), and <>. If you want to use some other character as your start delimiter and it doesn't have a "natural" partner for termination, you must use the same character for start and end delimiters.
Obviously you can't user [] delimiters because you have this in your queries. I sugest using {} delimiters.
Of course you can use double qoute in it it''s with replace. You can omit last parameter in replace because it isn't mandatory and without it it automatically will remove ' character.
INSERT INTO TAB_AUTOCRCMTREQUESTS (CommentText) VALUES (REPLACE('...it''s friday!', ''''))
Single quotes are escaped by doubling them up
INSERT INTO Blagh VALUES(REPLACE('it''s friday', '''', ''),12);
You can try this, (sorry but I don't know why q'[ ] works)
INSERT INTO TAB_AUTOCRCMTREQUESTS
(RequestOrigin, RequestKey, CommentText) VALUES ('Tracker', 'OPM03865_0', q'[[Orange.Security.OrangePrincipal] em[u02650791]okok it's friday!]')
I just got the q'[] from this link Oracle pl-sql escape character (for a " ' ") - this question could be a possible duplicate

I want fetch substring from in oracle table between last '/' and before '.' from last in images table

I want to fetch substring from string in column between last '/' and last '.' .
Here is sample date for IMAGE_PATH column name:
sph/images/30_Fairhall_Court.jpeg
sph/images/9_Pennethorne_House.jpeg
rbkc/images/TAVISTOCK_CRESCENT.jpeg
haringey/images/399932thumb.jpg
urbanchoice/images/18190862.jpg
wandle/images/f13c10d2-2692-457d-a208-8bb9e10b27dc.png
housingmoves/images/No14_Asterid Heights_DS37620.jpg
wandle/images/f13c10d2-2692-457d-a208-8bb9e10b27dc.png
So the required output is like
30_Fairhall_Court
9_Pennethorne_House
TAVISTOCK_CRESCENT
399932thumb
18190862
f13c10d2-2692-457d-a208-8bb9e10b27dc
No14_Asterid Heights_DS37620
f13c10d2-2692-457d-a208-8bb9e10b27dc
Please suggest how to fetch. I need to update another blank column in table with this value. The table has around 10 lacks records.
One of possible solutions is to use functions substr() and instr() with negative third parameter:
select image_path,
substr(image_path,
instr(image_path, '/', -1) + 1,
instr(image_path, '.', -1)-instr(image_path, '/', -1) - 1) img
from test
SQL Fiddle
Results:
IMAGE_PATH IMG
-------------------------------------------------------- -------------------------------------
sph/images/30_Fairhall_Court.jpeg 30_Fairhall_Court
sph/images/9_Pennethorne_House.jpeg 9_Pennethorne_House
rbkc/images/TAVISTOCK_CRESCENT.jpeg TAVISTOCK_CRESCENT
haringey/images/399932thumb.jpg 399932thumb
urbanchoice/images/18190862.jpg 18190862
wandle/images/f13c10d2-2692-457d-a208-8bb9e10b27dc.png f13c10d2-2692-457d-a208-8bb9e10b27dc
housingmoves/images/No14_Asterid Heights_DS37620.jpg No14_Asterid Heights_DS37620
wandle/ima.ges/f13c10d2-2692-457d-a208-8bb9e10b27dc.png f13c10d2-2692-457d-a208-8bb9e10b27dc
This regex works with the sample data you provided:
select regexp_substr(image_path
, '(/)([a-z0-9_ \-]+)(\.)([a-z]+)$'
, 1
, 1
, 'i'
, 2)
from t23
/
We have to include all the optional parameters after pattern so we can use the subexpr parameter to select just the filename element. Find out more.
As far as the updating goes, a million row table isn't that big. Given that you have to update all the rows there's not much you can do to tune it. Just issue the UPDATE statement and let it rip.
"its not working"
Hmmm, here's a SQL Fiddle which proves it does work. You've probably introduced a typo.
"The regexp looks unnecessary complex. Why not simply"
Perhaps it is too complicated. However your simplified version doesn't produce the correct result if there's more than one dot in the IMAGE_PATH. If that's never going to happen then your solution works just fine.

Oracle Pattern matching

In Oracle I want to check whether the string has "=' sign at the end. could you please let me know how to check it. If it has '=' sign at the end of string, I need to trailing that '=' sign.
for eg,
varStr VARCHAR2(20);
varStr = 'abcdef='; --needs to trailing '=' sign
I don't think you need "pattern matching" here. Just check if the last character is the =
where substr(varstr, -1, 1) = '='
substr when called with a negative position will work from the end of the string, so substr(varstr,-1,1) extracts the last character of the given string.
Use the REGEX_EXP function. I'm putting a sql command since you didn't specify on your question.:
select *
from someTable
where regexp_like( someField, '=$' );
The pattern $ means that the precedent character should be at the end of the string.
see it here on sql fiddle: http://sqlfiddle.com/#!4/d8afd/3
It seems that substr is the way to go, at lease with my sample data of about 400K address lines this returns 1043 entries that end in 'r' in an average of 0.2 seconds.
select count(*) from addrline where substr(text, -1, 1) = 'r';
On the other hand, the following returns the same results but takes 1.1 seconds.
select count(*) from addrline where regexp_like(text, 'r$' );

Oracle regex for all characters after a specific character

I need to get all characters after the last 'R' from a part number like this:
223A GHH R337716
So far I've got:
REGEXP_SUBSTR(CUST_PART_NO,'R(.*)')
This returns R337716, but I don't want the 'R' and I'm not sure if it would work if there was more than one 'R' in the string.
Then you have to replace the string with groups of expressions like this:
regexp_replace( CUST_PART_NO, '(.*)(R)(.*)', '\3' )
See it here on sqlfiddle: http://sqlfiddle.com/#!4/3ec77/2
What I'm saying with this expression is:
get everything until R make it as a group 1 expression the first (.*)
get the R make it the second group (R)
get everything else and make it the third group (.*)
The parenthesis on a regular expression define groups of expressions.
What about
ltrim(REGEXP_SUBSTR(CUST_PART_NO,'R(.*)'), 'R')

Whats the XPath equivalent to SQL In query?

I would like to know whats the XPath equivalent to SQL In query. Basically in sql i can do this:
select * from tbl1 where Id in (1,2,3,4)
so i want something similar in XPath/Xsl:
i.e.
//*[#id= IN('51417','1121','111')]
Please advice
(In XPath 2,) the = operator always works like in.
I.e. you can use
//*[#id = ('51417','1121','111')]
A solution is to write out the options as separate conditions:
//*[(#id = '51417') or (#id = '1121') or (#id = '111')]
Another, slightly less verbose solution that looks a bit like a hack, though, would be to use the contains function:
//*[contains('-51417-1121-111-', concat('-', #id, '-'))]
Literally, this means you're checking whether the value of the id attribute (preceeded and succeeded by a delimiter character) is a substring of -51417-1121-111-. Note that I am using a hyphen (-) as a delimiter of the allowable values; you can replace that with any character that will not appear in the id attribute.

Resources