I have a MySQL data table which stores metadata for client transactions. I am writing a query to extract a number out of the metadata column, which is essentially a JSON stored as a string.
I am trying to find ‘clients’ and extract the first number after clients. The data can be stored in several different ways; see the examples below:
..."type":"temp","typeOther":"","clients":"2","hours":"5",...
..."id":31457,"clients":2,"cancel":false...
I’ve tried the following Regexp:
(?<=clients.....)[0-9]+
(?<=clients...)[0-9]*(?=[[^:digit:]])
And I’ve tried the following json_extract, but it returned a null value:
json_extract(rd.meta, '$.clients')
The regexp functions do work, but the first one only works on the first example, while the second only works on the second example.
What regexp should I use such that it’s dynamic and will pull the number nested between two non-word char sets after ‘clients’?
2
Answers
I did this test on MySQL 8.0.29, but it should work on MySQL 5.x too:
Then cast the result as an integer to get rid of the non-numeric part following the number.