Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As I noted originally, while you can't use a user-defined function, you can't solve this problem. It is quite simply not doable.</p> <p>If you can use a function, then try this for size:</p> <pre><code>CREATE FUNCTION find_first_number(str VARCHAR(32), def INTEGER DEFAULT 0) RETURNING INTEGER AS number WITH(NOT VARIANT); DEFINE i INTEGER; DEFINE j INTEGER; DEFINE l INTEGER; DEFINE c CHAR(1); DEFINE nstr VARCHAR(32); IF str IS NULL THEN RETURN def; END IF; LET l = LENGTH(str); FOR i = 0 TO l LET c = SUBSTR(str, i, 1); IF c &gt;= "0" AND c &lt;= "9" THEN LET nstr = c; FOR j = i + 1 TO l LET c = SUBSTR(str, j, 1); IF c &gt;= "0" AND c &lt;= "9" THEN LET nstr = nstr || c; ELSE RETURN nstr; -- String of digits in middle of string END IF; END FOR; RETURN nstr; -- String of digits at end of string END IF; END FOR; RETURN def; END FUNCTION; </code></pre> <p>The <code>WITH(NOT VARIANT)</code> clause tells the optimizer that for the same input, the function always produces the same output (so the output is invariant for a given input).</p> <p>Test code:</p> <pre><code>CREATE TEMP TABLE ffn_test ( str VARCHAR(32), def INTEGER, num INTEGER ); INSERT INTO ffn_test VALUES("UNKNOWN", 0, 0); INSERT INTO ffn_test VALUES("ROOKIE", -1, -1); INSERT INTO ffn_test VALUES("LEVEL 1", 0, 1); INSERT INTO ffn_test VALUES("LEVEL 2A", 0, 2); INSERT INTO ffn_test VALUES("LEVEL 2B", 0, 2); INSERT INTO ffn_test VALUES("LEVEL 20", 0, 20); INSERT INTO ffn_test VALUES("LEVEL 999", 0, 999); INSERT INTO ffn_test VALUES("LEVEL 3.0", 0, 3); INSERT INTO ffn_test VALUES(NULL, 0, 0); SELECT str, def, num, find_first_number(str, def) AS result, CASE WHEN num = find_first_number(str, def) THEN "PASS" ELSE "FAIL" END pass_fail FROM ffn_test; </code></pre> <p>Test output:</p> <pre><code>UNKNOWN 0 0 0 PASS ROOKIE -1 -1 -1 PASS LEVEL 1 0 1 1 PASS LEVEL 2A 0 2 2 PASS LEVEL 2B 0 2 2 PASS LEVEL 20 0 20 20 PASS LEVEL 999 0 999 999 PASS LEVEL 3.0 0 3 3 PASS 0 0 0 PASS </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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