SQL to return results for the following regex

Go To StackoverFlow.com

1

I have the following regular expression:

WHERE A.srvc_call_id = '40750564' AND REGEXP_LIKE (A.SRVC_CALL_DN, '[^TEST]')

The row that contains 40750564 has "TEST CALL" in the column SRVC_CALL_DN and REGEXP_LIKE doesn't seem to be filtering it out. Whenever I run the query it returns the row when it shouldn't.

Is my regex pattern wrong? Or does SQL not accept [^whatever]?

2012-04-04 18:23
by Kyle


2

The carat anchors the expression to the start of a string. By enclosing the letters T, E, S & T in square brackets you're searching, as barsju suggests for any of these characters, not for the string TEST.

You say that SRVC_CALL_DN contains the string 'TEST CALL', but you don't say where in the string. You also say that you're looking for where this string doesn't match. This implies that you want to use not regexp_like(...

Putting all this together I think you need:

AND NOT REGEXP_LIKE (A.SRVC_CALL_DN, '^TEST[[:space:]]CALL')

This excludes every match from your query where the string starts with 'TEST CALL'. However, if this string may be in any position in the column you need to remove the carat - ^.

This also assumes that the string is always in upper case. If it's in mixed case or lower, then you need to change it again. Something like the following:

AND NOT REGEXP_LIKE (upper(A.SRVC_CALL_DN), '^TEST[[:space:]]CALL')

By upper-casing SRV_CALL_DN you ensure that you're always going to match but ensure that your query may not use an index on this column. I wouldn't worry about this particular point as regular expressions queries can be fairly poor at using indexes anyway and it appears as though SRVC_CALL_ID is indexed.

Also if it may not include 'CALL' you will have to remove this. It is best when using regular expressions to make your match pattern as explicit as possible; so include 'CALL' if you can.

2012-04-04 18:46
by Ben
Ah. That sound right.. I thought he was using ^ as the start of line. - barsju 2012-04-04 18:51
My intention was to exclude any rows with "test" in SRVCCALLDN. I used your suggestion with NON REGEXP_LIKE, which worked ! Thanks - Kyle 2012-04-05 12:18


1

Try with '^TEST' or '^TEST.*'

Your regexp means any string not starting with any of the characters: T,E,S,T.

But your case is so simple, starts with TEST. Why not use a simple like:

LIKE 'TEST%'
2012-04-04 18:26
by barsju
+1 I was so focused on the regexes I didn't consider like - Ben 2012-04-04 18:57
Ads