Home » SQL & PL/SQL » SQL & PL/SQL » regexp_replace. Add double quote(") to string of values (10.2)
regexp_replace. Add double quote(") to string of values [message #622044] Wed, 20 August 2014 11:19 Go to next message
lott42_gmail
Messages: 146
Registered: June 2010
Senior Member
I sometimes will check a list of values to see if one particular value exists within it.

For example
SELECT decode(instr('(-2, -1, 3)', '3'), 0, 'Not Exists', 'Exists') val
  FROM dual;


Result: Exists

but, if I want to see if "2" exists

SELECT decode(instr('(-2, -1, 3)', '2'), 0, 'Not Exists', 'Exists') val
  FROM dual;


Result: Exists

I really want only "2" to exist but not "-2" exists

If I try it with double quotes(") it seems to work the way I want it to

SELECT decode(instr('("-2", "-1", "3")', '"2"'), 0, 'Not Exists', 'Exists') val
  FROM dual;


Result: Not Exists

My question is this, Is there a regular expression that I can use to put the double quotes around each value. Each value will be listed in a comma delimited string such as '(-2, -1, 3)'. I can handle the other string such as '2' -> '"2"'??

For now, I used this code to do the same thing. It splits the comma delimited string into rows and does a join with the value I'm looking for. I'm not good enough at regex to accomplish this.

SELECT decode(COUNT(test_value.val), 0, 'Not Exists', 'Exists') val
  FROM (SELECT TRIM(regexp_substr(upper(val), '[^,]+', 1, LEVEL)) AS val
          FROM (SELECT regexp_replace('(-2, -1, 3)', '[()]') val FROM dual)
        CONNECT BY LEVEL <= length(regexp_replace(val, '[^,]')) + 1) possible_values,
       (SELECT 2 val FROM dual) test_value
 WHERE test_value.val = possible_values.val;


Result: Not Exists
Re: regexp_replace. Add double quote(") to string of values [message #622046 is a reply to message #622044] Wed, 20 August 2014 11:30 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
why do you have multiple values stored in single column?
why do you store number as character values?

If you normalized the data & used correct datatype, you would not have these problems.
Re: regexp_replace. Add double quote(") to string of values [message #622049 is a reply to message #622046] Wed, 20 August 2014 11:34 Go to previous messageGo to next message
lott42_gmail
Messages: 146
Registered: June 2010
Senior Member
Yes, fair assessment. The developers of one the systems used this technique to store the values. Instead of trying to rewrite a lot of code, I'm using their storage "technique" and trying to come up with quick solution to it.
Re: regexp_replace. Add double quote(") to string of values [message #622050 is a reply to message #622044] Wed, 20 August 2014 11:37 Go to previous messageGo to next message
thelearner
Messages: 133
Registered: April 2013
Location: INDIA
Senior Member
sorry wrong query updated

[Updated on: Wed, 20 August 2014 11:40]

Report message to a moderator

Re: regexp_replace. Add double quote(") to string of values [message #622056 is a reply to message #622049] Wed, 20 August 2014 11:55 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
lott42 wrote on Wed, 20 August 2014 22:04
trying to come up with quick solution to it.


Quick solution? You cannot have the following things together http://www.orafaq.com/forum/t/194115/
Re: regexp_replace. Add double quote(") to string of values [message #622071 is a reply to message #622044] Wed, 20 August 2014 13:59 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
SQL> SELECT CASE
  2           WHEN REGEXP_LIKE('(-2, -1, 3)','(^|\(|,| )' || '&value' || '(,|\)|$)') THEN 'Exists'
  3           ELSE 'Not Exists'
  4         END val
  5    FROM dual;
Enter value for value: 3
old   2:          WHEN REGEXP_LIKE('(-2, -1, 3)','(^|\(|,| )' || '&value' || '(,|\)|$)') THEN 'Exists'
new   2:          WHEN REGEXP_LIKE('(-2, -1, 3)','(^|\(|,| )' || '3' || '(,|\)|$)') THEN 'Exists'

VAL
----------
Exists

SQL> /
Enter value for value: 2
old   2:          WHEN REGEXP_LIKE('(-2, -1, 3)','(^|\(|,| )' || '&value' || '(,|\)|$)') THEN 'Exists'
new   2:          WHEN REGEXP_LIKE('(-2, -1, 3)','(^|\(|,| )' || '2' || '(,|\)|$)') THEN 'Exists'

VAL
----------
Not Exists

SQL> /
Enter value for value: -2
old   2:          WHEN REGEXP_LIKE('(-2, -1, 3)','(^|\(|,| )' || '&value' || '(,|\)|$)') THEN 'Exists'
new   2:          WHEN REGEXP_LIKE('(-2, -1, 3)','(^|\(|,| )' || '-2' || '(,|\)|$)') THEN 'Exists'

VAL
----------
Exists

SQL> 


SY.
Re: regexp_replace. Add double quote(") to string of values [message #622080 is a reply to message #622071] Wed, 20 August 2014 16:05 Go to previous messageGo to next message
lott42_gmail
Messages: 146
Registered: June 2010
Senior Member
Wow, Solomon, thanks. That's something like I was looking for. I'll be honest with you, those patterns are beyond my understanding. I need to spend more time on working with them.

Thanks Again!!
Re: regexp_replace. Add double quote(") to string of values [message #622091 is a reply to message #622071] Thu, 21 August 2014 00:50 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Juts for information, you don't need to concatenate &value, you can embed it directly in the string:
SQL> SELECT CASE
  2           WHEN REGEXP_LIKE('(-2, -1, 3)','(^|\(|,| )&value(,|\)|$)') THEN 'Exists'
  3           ELSE 'Not Exists'
  4         END val
  5    FROM dual;
Enter value for value: 3
VAL
----------
Exists

1 row selected.

SQL> /
Enter value for value: 2
VAL
----------
Not Exists

1 row selected.

SQL> /
Enter value for value: -2
VAL
----------
Exists

1 row selected.

Re: regexp_replace. Add double quote(") to string of values [message #622220 is a reply to message #622091] Fri, 22 August 2014 10:27 Go to previous messageGo to next message
lott42_gmail
Messages: 146
Registered: June 2010
Senior Member
Michel, thanks for sharing an additional variation. I appreciate it.
Re: regexp_replace. Add double quote(") to string of values [message #623809 is a reply to message #622091] Mon, 15 September 2014 02:32 Go to previous messageGo to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
hi michel/solomon, i was just searching this forum for regexp solutions , and trying to understand and learn , this particular solution i am not able to get it.Can someone explain me the logic.it would be of great help.



REGEXP_LIKE('(-2, -1, 3)','(^|\(|,| )&value(,|\)|$)')

--^|  position  start with or condition and consider the opening brackets ,| ,closing brackers by escaping them using \
--&value to be searched
-- (,|\) -- to refer the first captured or memorized group again with ","
--please explain.



Re: regexp_replace. Add double quote(") to string of values [message #623871 is a reply to message #623809] Mon, 15 September 2014 13:19 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
m.abdulhaq wrote on Mon, 15 September 2014 03:32
this particular solution i am not able to get it.


Well, first we identify (in green) groupings (groupings are enclosed in parenthesis) and (in red) pipe characters (pipe is a special character for OR operation):

'(^|\(|,| )&value(,|\)|$)'

First grouping:

'(^|\(|,| )

consists or three patterns:

1. ^ - beginning of line anchor
2. open parenthesis (we escape it with backslash since open parenthesis is a special character in regexp)
3. space

All three patters are "connected" by OR, therefore first grouping reads beginning of line or open parenthesis or space. Similar way second grouping reads comma or close parenthesis or end of line. So whole pattern reads: beginning of line or open parenthesis or space followed by value followed by comma or close parenthesis or end of line.

SY.
icon14.gif  Re: regexp_replace. Add double quote(") to string of values [message #623894 is a reply to message #623871] Tue, 16 September 2014 01:34 Go to previous message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
thank you very much Solomon for explanation , i really understood the solution now.I am also trying to understand the regex concepts from http://www.regular-expressions.info/tutorial.html . thanks for sharing wonderful information .
Previous Topic: Compare two rows from a same table
Next Topic: Oracle doesn't use a partial index
Goto Forum:
  


Current Time: Tue Apr 23 16:04:58 CDT 2024