Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two options:</p> <ol> <li>The first query uses two <code>REGEXP_LIKE</code> tests: one regular expression to generically match; and one for excluding the invalid matches.</li> <li>The second query uses <code>REGEXP_SUBSTR</code> to testfor a generic match and extract the sub-group of the match and then tests to see whether it should be exluded.</li> </ol> <p>The third query then looks at how you can extend the query by having another table containing the match criteria and allows you to build and test multiple name variants.</p> <p><a href="http://sqlfiddle.com/#!4/7000b/3" rel="nofollow">SQL Fiddle</a></p> <p><strong>Oracle 11g R2 Schema Setup</strong>:</p> <pre><code>CREATE TABLE tbl ( str ) AS SELECT 'Stephen' FROM DUAL UNION ALL SELECT 'Steven' FROM DUAL UNION ALL SELECT 'Stepen' FROM DUAL UNION ALL SELECT 'Steephen' FROM DUAL UNION ALL SELECT 'Steeven' FROM DUAL UNION ALL SELECT 'Steeven' FROM DUAL UNION ALL SELECT 'Smith' FROM DUAL UNION ALL SELECT 'Smithe' FROM DUAL UNION ALL SELECT 'Smythe' FROM DUAL UNION ALL SELECT 'Smythee' FROM DUAL; CREATE TABLE exclusions ( prefix, exclusion, suffix ) AS SELECT 'Ste', 'v|ph', 'en' FROM DUAL UNION ALL SELECT 'Sm', 'ithe?|ythe', '' FROM DUAL; </code></pre> <p><strong>Query 1</strong>:</p> <pre><code>SELECT str FROM tbl WHERE REGEXP_LIKE( str, '^Ste(\w+)en$' ) AND NOT REGEXP_LIKE( str, '^Ste(v|ph)en$' ) </code></pre> <p><strong><a href="http://sqlfiddle.com/#!4/7000b/3/0" rel="nofollow">Results</a></strong>:</p> <pre><code>| STR | |----------| | Stepen | | Steephen | | Steeven | | Steeven | </code></pre> <p><strong>Query 2</strong>:</p> <pre><code>SELECT str FROM (SELECT str, REGEXP_SUBSTR( str, '^Ste(\w+)en$', 1, 1, NULL, 1 ) AS match FROM tbl) WHERE match IS NOT NULL AND NOT REGEXP_LIKE( match, '^(v|ph)$' ) </code></pre> <p><strong><a href="http://sqlfiddle.com/#!4/7000b/3/1" rel="nofollow">Results</a></strong>:</p> <pre><code>| STR | |----------| | Stepen | | Steephen | | Steeven | | Steeven | </code></pre> <p><strong>Query 3</strong>:</p> <pre><code>SELECT str FROM tbl t WHERE EXISTS ( SELECT 1 FROM exclusions e WHERE REGEXP_LIKE( t.str, '^' || e.prefix || '(\w+)' || e.suffix || '$' ) AND NOT REGEXP_LIKE( t.str, '^' || e.prefix || '(' || e.exclusion || ')' || e.suffix || '$' ) ) </code></pre> <p><strong><a href="http://sqlfiddle.com/#!4/7000b/3/2" rel="nofollow">Results</a></strong>:</p> <pre><code>| STR | |----------| | Stepen | | Steephen | | Steeven | | Steeven | | Smythee | </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload