PostgreSQL provides a powerful regular expression system that enables fuzzy queries at the database level.
Regular expression matching operators:
operator |
Description |
Example |
~ |
Match regular expressions, case-related |
' Thomas ' ~ '. *thomas.* ' |
~* |
Match regular expression, case-insensitive |
' Thomas ' ~* '. *thomas.* ' |
!~ |
Mismatched regular expressions, case-related |
' Thomas '!~ '. *thomas.* ' |
!~* |
Mismatched regular expressions, case-insensitive |
' Thomas '!~* '. *vadim.* ' |
For example:
Find information for all user names in the data table account that contain Baidu and are case-insensitive.
SELECT * FROM account where username ~* ' Baidu ';
The use of regular expressions allows for case-insensitive functionality and greatly reduces the length of SQL statements.
Excerpt from: Http://blog.163.com/[email protected]/blog/static/1113522592010102215419516/
PostgreSQL query is case insensitive