

PostgreSQL automatically create indexes for columns which are Primary Keys, Unique, etc. !~~* for NOT ILIKE Indexing and LIKE/LIKE It matches any strings which contain an underscore.

Underscores can also be mapped in the same way: SELECT string FROM string_collection WHERE string LIKE '%\_%' This query is equivalent to: “Match all strings which ends with a % sign”.

To do so, we have to escape them using the default escape character – backslash.įor example: SELECT string FROM string_collection WHERE string LIKE '%\%'
#LIKE VS ILIKE SQL HOW TO#
Now an important question arises: How to match a % or _ itself? LIKE and ILIKE can be preceded with NOT to get the reverse effect: SELECT string FROM string_collection WHERE string NOT LIKE 'O%' Ī % without any other character matches all strings: SELECT string FROM string_collection WHERE string LIKE '%' SELECT string FROM string_collection WHERE lower(string) LIKE ‘o%’ Ī performance comparison of LIKE with lower vs ILIKE can be read here. The same effect can be achieved using the ‘lower()’ function and LIKE of PostgreSQL. ILIKE is similar to LIKE in all aspects except in one thing: it performs a case in-sensitive matching: SELECT string FROM string_collection WHERE string ILIKE 'O%' It is also possible to combine _ and % to get the desired result: SELECT string FROM string_collection WHERE string LIKE '_n%' In plain English, “It matches all strings of three characters whose middle character is small letter n”. įollowing query illustrate the use of _: SELECT string FROM string_collection WHERE string LIKE '_n_' It matches all strings beginning with ‘O’. _ in a pattern matches any single character.įor instance, consider the following query: SELECT string FROM string_collection WHERE string LIKE 'O%' % sign in a pattern matches any sequence of zero or more characters. Two of the important selectors in pattern matching with LIKE/ILIKE are the percentage sign(%) and underscore(_). To begin with, we will create a tiny table with few random string values. LIKE is the SQL standard while ILIKE is a useful extension made by PostgreSQL. LIKE and ILIKE are used for pattern matching in PostgreSQL.
