Home > Software design >  extract multiple matches from varchar or text column as rows from MariaDB
extract multiple matches from varchar or text column as rows from MariaDB

Time:01-23

I have a string (varchar ) column in my DB and I would like to use SQL to so some simple group matching and extract the matches into rows. Is there a way to accomplish this in plain SQL in mariaDB without a stored procedure or custom function?

Example:

 my_string ="this is a test string with x12345 and y1264 ...";

I am looking for something like this to extract all numbers starting with x or y into rows.

SELECT REGEXP_SUBSTR("[xy][0-9] ") from my_string;

Expected result:

x12345
y1264

The reason I need the rows is that this will be part of a bigger query where I join these rows on a key in another table.

My above query only returns 1 row, the 1st result x12345

Is there a way to obtain all matches?

CodePudding user response:

You may use JSON_TABLE to extract all words in your string, then check if these words start with x or y and are followed by numbers.

You can simply convert your string to a JSON format by adding enclosing it with square brackets, enclosing each word with double quotations, and replacing every single space with a comma. So you can use this code to do this CONCAT('["', REPLACE(myTxt, ' ', '","'), '"]').

Then you can use the JSON_TABLE as the following:

create table tbl(id int, myTxt TEXT);
insert into tbl values
  (1, 'this is a test string with x12345 and y1264'), 
  (2, 'this is a test xtest ytest string with x11111 and y11111 and x22222 and y22222');


SELECT W.xy_val
FROM tbl
CROSS JOIN JSON_TABLE
  (
    CONCAT('["', REPLACE(myTxt, ' ', '","'), '"]'), 
    '$[*]' 
    COLUMNS 
      (
       rowid FOR ORDINALITY, 
       xy_val VARCHAR(32) PATH '$'
      )
  ) W
WHERE  W.xy_val REGEXP '[x][0-9]' OR W.xy_val REGEXP '[y][0-9]'

See demo

  • Related