Home » SQL & PL/SQL » SQL & PL/SQL » Doubt on REGEXP_LIKE (Oracle 11g)
Doubt on REGEXP_LIKE [message #624234] Fri, 19 September 2014 06:02 Go to next message
na.dharma@gmail.com
Messages: 82
Registered: May 2008
Location: bangalore
Member

WITH CIF AS (SELECT '2477882_COLL_1' AS VAL FROM DUAL UNION ALL
SELECT '2794409_COLL_1' FROM DUAL
UNION ALL
SELECT '2291_Coll_1' FROM DUAL
UNION ALL
SELECT '2291' FROM DUAL)
SELECT * FROM CIF WHERE
VAL = CASE WHEN VAL LIKE '%\_1%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_2%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_3%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_4%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_5%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_6%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_7%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_8%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_9%' ESCAPE '\' THEN VAL
END
Re: Doubt on REGEXP_LIKE [message #624235 is a reply to message #624234] Fri, 19 September 2014 06:03 Go to previous messageGo to next message
na.dharma@gmail.com
Messages: 82
Registered: May 2008
Location: bangalore
Member

How to replace Case statement into REGEXP_LIKE, I mean instead of like, i want to use REGEXP_LIKE
Re: Doubt on REGEXP_LIKE [message #624236 is a reply to message #624235] Fri, 19 September 2014 06:15 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Did you look at my reply to Doubt on substr? I formatted my code, used code tags and then I posted TO HELP YOU. You need help, but look how unprofessional your questions are.
Re: Doubt on REGEXP_LIKE [message #624237 is a reply to message #624236] Fri, 19 September 2014 06:25 Go to previous messageGo to next message
na.dharma@gmail.com
Messages: 82
Registered: May 2008
Location: bangalore
Member

Hi Lalit, now you have formated code

WITH CIF AS (SELECT '2477882_COLL_1' AS VAL FROM DUAL UNION ALL
SELECT '2794409_COLL_1' FROM DUAL
UNION ALL
SELECT '2291_Coll_1' FROM DUAL
UNION ALL
SELECT '2291' FROM DUAL)
SELECT * FROM CIF WHERE 
VAL = CASE WHEN VAL LIKE '%\_1%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_2%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_3%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_4%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_5%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_6%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_7%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_8%' ESCAPE '\' THEN VAL
WHEN VAL LIKE '%\_9%' ESCAPE '\' THEN VAL
END 


How to replace Case statement into REGEXP_LIKE, I mean instead of like, i want to use REGEXP_LIKE
Re: Doubt on REGEXP_LIKE [message #624240 is a reply to message #624237] Fri, 19 September 2014 06:47 Go to previous messageGo to next message
na.dharma@gmail.com
Messages: 82
Registered: May 2008
Location: bangalore
Member

Any help on this
Re: Doubt on REGEXP_LIKE [message #624242 is a reply to message #624240] Fri, 19 September 2014 07:07 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
SQL> WITH CIF AS
  2    (SELECT '2477882_COLL_1' AS VAL FROM DUAL
  3    UNION ALL
  4    SELECT '2794409_COLL_1' FROM DUAL
  5    UNION ALL
  6    SELECT '2291_Coll_1' FROM DUAL
  7    UNION ALL
  8    SELECT '2291' FROM DUAL
  9    UNION ALL
 10    SELECT '2291_a' FROM DUAL
 11    UNION ALL
 12    SELECT '2291_1_a' FROM DUAL
 13    )
 14  SELECT * FROM CIF
 15  WHERE REGEXP_LIKE(VAL,'_[[:digit:]]')
 16  /

VAL
--------------
2477882_COLL_1
2794409_COLL_1
2291_Coll_1
2291_1_a


Can you please explain why are you using ESCAPE?
Re: Doubt on REGEXP_LIKE [message #624253 is a reply to message #624242] Fri, 19 September 2014 10:12 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Lalit Kumar B wrote on Fri, 19 September 2014 08:07
Can you please explain why are you using ESCAPE?


Read what escape does and you'll know why. And your regexp pattern is wrong. Should be:

REGEXP_LIKE(VAL,'_[1-9]')

SY.

[Updated on: Fri, 19 September 2014 10:13]

Report message to a moderator

Re: Doubt on REGEXP_LIKE [message #624255 is a reply to message #624253] Fri, 19 September 2014 10:27 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
@SY,

Either I am wrong or you have misinterpreted, because I just see "_" underscore as a wildcard character in OP's input. And I don't see a reason to use escape. Could you please explain where you think I am wrong, it would be good to know.

Thanks.

Edit : Regarding the regular expression, initially I thought of [0-9] class, but then I posted digit class. I would be happy to know why you think otherwise, since I have no clue from OP's input data.

[Updated on: Fri, 19 September 2014 10:32]

Report message to a moderator

Re: Doubt on REGEXP_LIKE [message #624256 is a reply to message #624255] Fri, 19 September 2014 10:35 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Did you read LIKE documentation. Like wildcards are % - any number of any characters and _ - any character. Without escape _2 means any character followed by digit 2 with escape it is underscore followed by digit 2.

SY.
Re: Doubt on REGEXP_LIKE [message #624257 is a reply to message #624256] Fri, 19 September 2014 10:43 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Ok, got it, you mean to say " any character. Without escape". Actually I got confused when you said "Read what escape does and your regexp is wrong". I read it as a single statement.

Edit : In fact, I am sorry, I was the one who misinterpreted it.

[Updated on: Fri, 19 September 2014 10:47]

Report message to a moderator

Re: Doubt on REGEXP_LIKE [message #624258 is a reply to message #624257] Fri, 19 September 2014 11:00 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
By "your regexp is wrong" I meant the following: you are using :digit: which is 0-9 while OP's CASE statement looks for 1-9 only.

SY.
Re: Doubt on REGEXP_LIKE [message #624259 is a reply to message #624258] Fri, 19 September 2014 11:18 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
But, if OP has values such as "_0", digit would suffice.

Quote:
OP's CASE statement looks for 1-9 only.


I initially thought of digits ranging anywhere in [1-9], but my thought was to make it generic.

[Updated on: Fri, 19 September 2014 11:20]

Report message to a moderator

Previous Topic: Doubt on Substr
Next Topic: Table LOCK in PeopleSoft Program
Goto Forum:
  


Current Time: Wed Apr 17 20:26:44 CDT 2024