Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Athena SQL with regex

I'm using DbVisualizer to connect to an athena instance. I have a working query:

SELECT device, description, id, size, date FROM test.database WHERE month = '01'
and device not like '%link%'
and device not like '%Link%'
and device not like '%LINK%'
and id not like '%abc%'
and id not like '%Abc%'
and id not like '%ABC%'
group by device, description, id, size, date order by month desc 

What I would like to do is clean it up and catch the cases using regex. I'm pretty sure /link.*/ig and /abc.*/ig would catch the case changes it but I don't know how to insert it in. I could not get "input.regex" = to work either.

like image 818
chowpay Avatar asked Feb 03 '26 06:02

chowpay


2 Answers

You may use REGEXP_LIKE here:

SELECT DISTINCT device, description, id, size, date
FROM test.database
WHERE
    month = '01' AND
    NOT REGEXP_LIKE(device, '[lL]ink|LINK') AND
    NOT REGEXP_LIKE(device, '[aA]bc|ABC')
ORDER BY
    month DESC;

Note that your GROUP BY logic can also just be represented by a distinct select, for which I have opted above.

like image 67
Tim Biegeleisen Avatar answered Feb 05 '26 22:02

Tim Biegeleisen


Assuming you want to just ignore case, you could probably simplify the suggestion from Tim Biegeleisen by writing:

SELECT DISTINCT device, description, id, size, date
FROM test.database
WHERE
    month = '01' AND
    NOT REGEXP_LIKE(device, '(?i)link|abc') 
ORDER BY
    month DESC;
like image 40
Christian von Wendt-Jensen Avatar answered Feb 05 '26 22:02

Christian von Wendt-Jensen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!