How to search the string in query with case insensitive on Clickhouse database?

孤者浪人 提交于 2021-02-08 17:22:38

问题


I am developing site using PHP and backend ClickHouse database. When i using like queries , it is not supporting case-sensitive words.

select id,comments from discussion where  comments LIKE "%Data not reflect%";

Is there any way to search case-insensitive words?


回答1:


There's no ILIKE operator. I think you can use lowerUTF8().

select id,comments from discussion where lowerUTF8(comments) LIKE '%Data not reflect%';

However, might be performance heavy as it will have to convert all comments values to lowercase.




回答2:


Use positionCaseInsensitive or positionCaseInsensitiveUTF8

Just like that

SELECT id,comments
FROM discussion
WHERE positionCaseInsensitive(comments,'Data not reflect')>0;

For more complicated patterns you can use regular expression with i flag inside:

SELECT ... WHERE match(comment, '(?i)Data.*not reflect');

See documentation: https://clickhouse.yandex/docs/en/query_language/functions/string_search_functions/#position-haystack-needle-locate-haystack-needle




回答3:


Case insensitive ILIKE operator was added to CH starting with version 20.6.3.28:

SELECT *
FROM
(
    SELECT '** Data not reflect **' AS text
    UNION ALL
    SELECT '** data not reflect **'
)
WHERE text ILIKE '%Data not reflect%'

/*
┌─text───────────────────┐
│ ** Data not reflect ** │
│ ** data not reflect ** │
└────────────────────────┘
*/


来源:https://stackoverflow.com/questions/58728436/how-to-search-the-string-in-query-with-case-insensitive-on-clickhouse-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!