I have a table with multiple columns that I have to look through to check for this value: '[m=' and return the numbers within that list.
For example lets say I have this in the 'name' column xyzzy [m=123] I want to return 123.
I was trying the below query but its not working because I keep seeing the following error:
SQL compilation error: syntax error line 3 at position 4 unexpected 'WHEN'. syntax error line 3 at position 34 unexpected ')'.
I also think SPLIT_PART isn't going to work because then if I have xyzzy [m=123] it will return 123] and I don't want the closing bracket included
case
WHEN REGEXP_SUBSTR(name, '[m=') THEN SPLIT_PART(name, '[m=', )
WHEN REGEXP_SUBSTR(name2, '[m=') THEN SPLIT_PART(name2, '[m=', )
WHEN REGEXP_SUBSTR(name3, '[m=') THEN SPLIT_PART(name3, '[m=', )
WHEN REGEXP_SUBSTR(name4, '[m=') THEN SPLIT_PART(name4, '[m=', )
WHEN REGEXP_SUBSTR(name5, '[m=') THEN SPLIT_PART(name5, '[m=', )
WHEN REGEXP_SUBSTR(name6, '[m=') THEN SPLIT_PART(name6, '[m=', )
else null;
+------+--------+---------+------------+------+----------------+---------------+
| name | name2 | name3 | name3 | name5 | name6 |
+------+--------+---------+------------+------+----------------+---------------+
| xyzzy [m=123] | MyISAM | 10 | Fixed | 0 | my comment |
+------+--------+---------+------------+------+----------------+---------------+
| rts3 | MyISAM | 1 | test [m=122] | 4 | my comment |
+------+--------+---------+------------+------+----------------+---------------+
| rddts3 | MyISAM | 1 | dm32dfe | 4 | comment [m=177] |
+------+--------+---------+------------+------+----------------+---------------+
*** EDIT: I've tried adjusting my query to look like this:
Select *,
CASE
WHEN REGEXP_SUBSTR(NAME, '(?:\[m=)') THEN REGEXP_SUBSTR(NAME, '[[]m=([0-9]+)'),
WHEN REGEXP_SUBSTR(NAME2, '(?:\[m=)') THEN REGEXP_SUBSTR(NAME2, '[[]m=([0-9]+)'),
ELSE null
END
from my_table
but now I'm seeing this error: SQL compilation error: error line 2 at position 0 Invalid argument types for function 'IFF': (VARCHAR(16777216), VARCHAR(16777216), NULL)