1

I need to find all the tables in our Oracle database that have attributes that match a certain word. So for example I am using:

SELECT TABLE_NAME
     , COLUMN_NAME
  FROM USER_TAB_COLUMNS
 WHERE UPPER(COLUMN_NAME) LIKE '%ING%' 

This returns:

TABLE1 ING
TABLE2 THIS_ING
TABLE3 ING_FIRST
TABLE5 TESTING

I only want to return the results from TABLE1, TABLE2 and TABLE3. I don't want a match when it is only part of a string like TESTING.

I can't seem to get exactly what I need.

6
  • 2
    Hah, you are one those peculiar species that place the comma to precede the item on the next line. Commented Apr 26, 2012 at 15:38
  • 2
    are you also one of these species? haha, I picked up that habit from a friend who is a 20 year DBA veteran. It just seems to line up better when I do that. Additionally, you can comment out the attribute easier, you don't also have to comment out the comma on the preceding line if you happening to be commenting out the last attribute. Commented Apr 26, 2012 at 15:39
  • Good point. I am implementing this from today onwards. Commented Apr 26, 2012 at 15:44
  • You do, however, have the opposite problem - you have to comment out the comma on the next line if you're commenting out the first item. Commented Apr 26, 2012 at 16:17
  • 2
    The leading comma is a SQL best practice. Commented Apr 26, 2012 at 16:18

2 Answers 2

8

You can use a regular expression to find what you want:

SELECT TABLE_NAME
     , COLUMN_NAME
  FROM USER_TAB_COLUMNS
 WHERE regexp_like (column_name, '(^|(_)+)ING((_)+|$)')

This will give you columns that have ING preceded by either start of line or 1 ore more underscores, and followed by the end of line or 1 ore more underscores.

Sign up to request clarification or add additional context in comments.

6 Comments

This is it!! Thanks for the help, this site never fails me.
+1 nice that didn't take long for someone who actually knew regex, although I'm curious why not just (^|(_)+)ING
@ConradFrix I don't know if I'd go as far as saying I "know" regex, but given enough time, effort and luck I can hack something together (started on it before your answer was posted).
@ConradFrix Your example also pulls "INGRID", which I don't think the OP would want to see.
@DanA. Ah I see. I'm not sure if that's required but that's fine to go the extra mile
|
1

You could use REGEXP_LIKE. I'm not terribly good at regex, someone better than me could figure out how to do either start of a word or _ in one expression

SELECT TABLE_NAME
 ,     COLUMN_NAME
FROM USER_TAB_COLUMNS
WHERE
REGEXP_LIKE(COLUMN_NAME,'[_]ING')
or
REGEXP_LIKE(COLUMN_NAME,'^ING')

DEMO

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.