Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do you escape underscores and single quotes in Amazon Athena queries?

I would like to run a query like the following on Amazon Athena

Select * from my_table
where my_table.my_field like '%'sample_text'%'

I want to match the single quotes and the underscore in ‘sample_text’.

I’ve tried variations of escape characters like \_, \\_, [_], `_, and `_` without success.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is this possible?

>Solution :

To escape special characters in LIKE use ESCAPE parameter:

Wildcard characters can be escaped using the single character specified for the ESCAPE parameter.

WITH dataset (str) AS (
    VALUES ('sample_text '),
    ('sample text ')
)

SELECT *
FROM dataset 
WHERE str like 'sample\_text%' ESCAPE '\'

Output:

str
sample_text
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading