如何在 Amazon Athena 查询中转义下划线和单引号?

问题描述 投票:0回答:1

我想在 Amazon Athena 上运行如下查询

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

我想匹配'sample_text'中的单引号和下划线。

我尝试过各种转义字符,例如 \_、\_、[_]、`_ 和 `_`,但没有成功。

这可能吗?

sql amazon-athena presto trino
1个回答
5
投票

要转义

LIKE
中的特殊字符,请使用
ESCAPE
参数:

可以使用为

ESCAPE
参数指定的单个字符对通配符进行转义。

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

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

输出:

str
样本文本
© www.soinside.com 2019 - 2024. All rights reserved.