Trying to apply regex to column in select query - mysql

I have JSON stored in a MySQL database (version 5.6.17) that I'm trying to regex into a column to retrieve a list of campaign IDs. My query is as follows:
SELECT JSON REGEXP '"id":([0-9]*)' AS id
FROM PROD_APPNEXUS.dimension_json_creatives;
where JSON is a column containing the data I need to parse as ID. I know REGEXP can be used for strings in SELECT queries (i.e. SELECT 'foobar' REGEXP '([a-z]+)' AS foobar) but can columns be pattern matched in the same way?
Would there be a way to cast the JSON column as string and then regex?
Any help would be appreciated!
Thanks,
Sam

You can use replace and substring_index to split your column, like this;)
SELECT replace(substring_index(JSON, ':', -1), '"', '') AS id
FROM PROD_APPNEXUS.dimension_json_creatives;
when I run sql below return aaaa,
select replace(substring_index('"id":"aaaa"', ':', -1), '"', '');
I assumed your JSON's value does not exist :.

Related

I need to split a mysql field (string) by multiple delimeters into json object

I inherited a mysql database and am trying to migrate it to mongodb. There is a field called details that contains some key value "pairs" I want to split up. There could be a single key/value pair, or multiple pairs split by multiple delimiters. I put pairs in quotes because they are formatted strangely. They are delimited by colon : and key values split by commas ,. For example here is the value of one such field:
Normal Duty,5min:Heavy Duty,10min:Riser,10max:
This is 3 key value pairs, delimited by the colon. I want to get these into a json object if possible, like this:
{
'Normal Duty': '5min',
'Heavy Duty': '10min',
'Riser': '10max'
}
I think I could do it using substring_index if it were only a single key/value pair that had a single delimiter, but I get lost trying to think of a way to extract multiple key/value pairs with multiple delimiters. I'm able to get a count of the number of delimiters, SELECT id, details, LENGTH(details) - LENGTH(REPLACE(details, ':', '')) AS COUNT FROM type but not sure how I could use that number in a loop or something.
SELECT test.id,
JSON_OBJECTAGG(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(test.value, ':', numbers.num), ':', -1), ',', 1),
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(test.value, ':', numbers.num), ':', -1), ',', -1))
FROM test
CROSS JOIN ( SELECT 1 num UNION SELECT 2 UNION SELECT 3 UNION
SELECT 4 UNION SELECT 5 UNION SELECT 6 ) numbers
WHERE numbers.num <= LENGTH(test.value) - LENGTH(REPLACE(test.value, ':', ''))
GROUP BY test.id
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=d10261e21a0fb1c1060091e8b4e58d80
Adjust numbers subquery if the amount of key-value pairs may be above 6.
PS. To work with JSON I'd strongly recommend to upgrade your server.

MySql Substring Index, Find and replace characters

I need to find the first and second "_" and extract whatever is between.
example data
doc_856_abc_123
doc_876_xyz_999
So far I have the following substring query. But I need help
select SUBSTRING_INDEX( column, '_', 2 )
It is outputting
doc_856
doc_867
How do I combine the above query to maybe another substring go get the desired results. Which would be.
856
867
Just apply SUBSTRING_INDEX again on the resulted string
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(column, '_', 2 ), '_', -1)

Getting unique entries from a columns generated by matching regexp in SQL

I have a table which i am using to query and getting its one column which matches regular expression which is (\/.+\/\?).
Content of the resulted column is like:
/Anything here/?
Example output:
\abc\cdf\?....
\ab\?....
\abc\cdf\?....
\sb\?....
where '....' can be anything
Desired result i want is unique values before \? such that rows with duplicate regexp matched content are shown once only like here (\abc\cdf\?.... showing twice instead of onece)
\abc\cdf\?....
\ab\?....
\sb\?....
OR
\abc\cdf\?
\ab\?
\sb\?
I have looked very much but couldn't find anything there is regexp_substr in oracle but that is not working in SQL.
Please if someone could help me with the sql query that would be awesome.
If you want everything before the last \, then you can use substring_index() and some string manipulation:
select substring_index(col, '\\',
length(col) - length(replace(col, '\\', ''))
) as firstpart,
count(*)
from table t
group by substring_index(col, '\\',
length(col) - length(replace(col, '\\', ''))
);

MySQL - extract number from data field using SUBSTRING

I'd like to extract the number between NUMBER and ;. So far I can extract the data up to the number, but I don't want anything after the number. e.g.,
SELECT
SUBSTRING(field, LOCATE('NUMBER=', rrule) + 7)
FROM table
Data field:
DATA:PASS=X12;NUMBER=331;FIELD=1
DATA:PASS=X12;NUMBER=2;FOO=BAR;FIELD=1
Desired Output:
331
2
You can use a combination of SUBSTRING_INDEX functions:
SELECT
SUBSTRING_INDEX(
SUBSTRING_INDEX(field, 'NUMBER=', -1),
';',
1)
FROM
tablename
Please see an example fiddle here.
The inner SUBSTRING_INDEX will return everything after the NUMBER= string, while the second will return everything before the ; returned by the inner function.

How to remove commas of integer from MY SQL select query

I am using a mySQL statement that returns me average of values as comma separated integer.
Eg : 2,109. But I want my output to be plain integer like 2109. Please help me on this.
You can use something like this:
SELECT REPLACE(fieldname, ',', '')
FROM ...
Or if type of fieldname is integer use this query
SELECT REPLACE(CONCAT(fieldname), ',', '')
FROM ...