Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not quite clear on whether you want exact matches or sub-string matches so I've included various options:</p> <p><a href="http://sqlfiddle.com/#!4/6b970/22" rel="noreferrer">SQL Fiddle</a></p> <p><strong>Oracle 11g R2 Schema Setup</strong>:</p> <pre><code>CREATE TABLE MY_TABLE (MY_COLUMN) AS SELECT '456' FROM DUAL UNION ALL SELECT '12345678' FROM DUAL UNION ALL SELECT 'abc 123 def 456' FROM DUAL; </code></pre> <p><strong>Query 1</strong>:</p> <p>If you only want rows where <code>MY_COLUMN</code> contains exactly a 3-digit number then you can just use your regular expression wrapped in the start-string (<code>^</code>) and end-string (<code>$</code>) anchors:</p> <pre><code> SELECT MY_COLUMN FROM MY_TABLE WHERE REGEXP_LIKE( MY_COLUMN, '^[[:digit:]]{3}$' ) </code></pre> <p><strong><a href="http://sqlfiddle.com/#!4/6b970/22/0" rel="noreferrer">Results</a></strong>:</p> <pre><code>| MY_COLUMN | |-----------| | 456 | </code></pre> <p><strong>Query 2</strong>:</p> <p>Or, if you are using Oracle 11g then you can use the less verbose PERL syntax:</p> <pre><code> SELECT MY_COLUMN FROM MY_TABLE WHERE REGEXP_LIKE( MY_COLUMN, '^\d{3}$' ) </code></pre> <p><strong><a href="http://sqlfiddle.com/#!4/6b970/22/1" rel="noreferrer">Results</a></strong>:</p> <pre><code>| MY_COLUMN | |-----------| | 456 | </code></pre> <p><strong>Query 3</strong>:</p> <p>If you want to extract the first 3-digit number from the column (where it can have surrounding text or more digits), then:</p> <pre><code> SELECT MY_COLUMN, REGEXP_INSTR( MY_COLUMN, '\d{3}' ), REGEXP_SUBSTR( MY_COLUMN, '\d{3}' ) FROM MY_TABLE WHERE REGEXP_LIKE( MY_COLUMN, '\d{3}' ) </code></pre> <p><strong><a href="http://sqlfiddle.com/#!4/6b970/22/2" rel="noreferrer">Results</a></strong>:</p> <pre><code>| MY_COLUMN | REGEXP_INSTR(MY_COLUMN,'\D{3}') | REGEXP_SUBSTR(MY_COLUMN,'\D{3}') | |-----------------|---------------------------------|----------------------------------| | 456 | 1 | 456 | | 12345678 | 1 | 123 | | abc 123 def 456 | 5 | 123 | </code></pre> <p><strong>Query 4</strong>:</p> <p>If you want to extract the first exactly 3-digit number from the column then:</p> <pre><code> SELECT MY_COLUMN, REGEXP_SUBSTR( REGEXP_SUBSTR( MY_COLUMN, '(^|\D)\d{3}(\D|$)' ), '\d{3}' ) AS match FROM MY_TABLE WHERE REGEXP_LIKE( MY_COLUMN, '(^|\D)\d{3}(\D|$)' ) </code></pre> <p><strong><a href="http://sqlfiddle.com/#!4/6b970/22/3" rel="noreferrer">Results</a></strong>:</p> <pre><code>| MY_COLUMN | MATCH | |-----------------|-------| | 456 | 456 | | abc 123 def 456 | 123 | </code></pre> <p><strong>Query 5</strong>:</p> <p>If you want to extract all the non-overlapping 3-digit numbers from the column (where it can have surrounding text), then:</p> <pre><code> WITH re_counts AS ( SELECT MY_COLUMN, REGEXP_COUNT( MY_COLUMN, '\d{3}' ) AS re_count FROM MY_TABLE ) ,indexes AS ( SELECT LEVEL AS "index" FROM DUAL CONNECT BY LEVEL &lt;= (SELECT MAX( re_count) FROM re_counts) ) SELECT MY_COLUMN, "index", REGEXP_SUBSTR( MY_COLUMN, '\d{3}', 1, "index" ) FROM re_counts INNER JOIN indexes ON ("index" &lt;= re_count) ORDER BY MY_COLUMN, "index" </code></pre> <p><strong><a href="http://sqlfiddle.com/#!4/6b970/22/4" rel="noreferrer">Results</a></strong>:</p> <pre><code>| MY_COLUMN | INDEX | REGEXP_SUBSTR(MY_COLUMN,'\D{3}',1,"INDEX") | |-----------------|-------|--------------------------------------------| | 12345678 | 1 | 123 | | 12345678 | 2 | 456 | | 456 | 1 | 456 | | abc 123 def 456 | 1 | 123 | | abc 123 def 456 | 2 | 456 | </code></pre> <p><strong>Query 6</strong>:</p> <p>If you want to extract all the sub-matches which are exactly 3-digit numbers then:</p> <pre><code> WITH re_counts AS ( SELECT MY_COLUMN, REGEXP_COUNT( MY_COLUMN, '(^|\D)\d{3}(\D|$)' ) AS re_count FROM MY_TABLE ) ,indexes AS ( SELECT LEVEL AS "index" FROM DUAL CONNECT BY LEVEL &lt;= (SELECT MAX( re_count) FROM re_counts) ) SELECT MY_COLUMN, "index", REGEXP_SUBSTR( REGEXP_SUBSTR( MY_COLUMN, '(^|\D)\d{3}(\D|$)', 1, "index" ), '\d{3}' ) AS match FROM re_counts INNER JOIN indexes ON ("index" &lt;= re_count) ORDER BY MY_COLUMN, "index" </code></pre> <p><strong><a href="http://sqlfiddle.com/#!4/6b970/22/5" rel="noreferrer">Results</a></strong>:</p> <pre><code>| MY_COLUMN | INDEX | MATCH | |-----------------|-------|-------| | 456 | 1 | 456 | | abc 123 def 456 | 1 | 123 | | abc 123 def 456 | 2 | 456 | </code></pre> <p><strong>Query 7</strong>:</p> <p>If you want to extract all 3-digit numbers from a column regardless of whether those matches partially overlap or not then:</p> <pre><code> WITH positions AS ( SELECT LEVEL AS pos FROM DUAL CONNECT BY LEVEL &lt;= (SELECT MAX( LENGTH( MY_COLUMN ) - 2 ) FROM MY_TABLE ) ) SELECT MY_COLUMN, pos, SUBSTR( MY_COLUMN, pos, 3 ) FROM MY_TABLE INNER JOIN positions ON (pos &lt;= LENGTH( MY_COLUMN ) - 2 ) WHERE REGEXP_LIKE( SUBSTR( MY_COLUMN, pos, 3 ), '^\d{3}$' ) ORDER BY MY_COLUMN, pos </code></pre> <p><strong><a href="http://sqlfiddle.com/#!4/6b970/22/6" rel="noreferrer">Results</a></strong>:</p> <pre><code>| MY_COLUMN | POS | SUBSTR(MY_COLUMN,POS,3) | |-----------------|-----|-------------------------| | 12345678 | 1 | 123 | | 12345678 | 2 | 234 | | 12345678 | 3 | 345 | | 12345678 | 4 | 456 | | 12345678 | 5 | 567 | | 12345678 | 6 | 678 | | 456 | 1 | 456 | | abc 123 def 456 | 5 | 123 | | abc 123 def 456 | 13 | 456 | </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. 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