Introduction to PostgreSQL Like
Whenever you are dealing with string manipulation in PostgreSQL many times it need to match the strings with a particular pattern and then performing some action and operation on the string if match successes are performed. This is one of the most common use-cases when it comes to string comparisons and manipulation. The traditional way to perform pattern matching that is used in SQL is by using the LIKE expression. There are many other methods introduced in PostgreSQL for pattern matching such as POSIX regular expression matching and SIMILAR TO expressions that can also be used for pattern matching. In this article, we will learn about the syntax of LIKE expression and how we can use it in PostgreSQL to perform pattern matching in strings.
Syntax of PostgreSQL Like
demoString LIKE anyPattern [ESCAPE characterThatNeedsToBeEscaped]
demoString NOT LIKE anyPattern [ESCAPE characterThatNeedsToBeEscaped]
DemoString: It can be any string value or the column of the particular table that stores string and that you want to check whether matches a pattern or contains some characters or substring.
AnyPattern: The pattern is a particular string that you want to match with the demoString. It may contain the %(percentage) sign and _(underscore) sign that help in defining the pattern. The underscore sign mentions that any character might be present on that particular position of the pattern string and % specifies the presence of one or more characters before or after or in-between the pattern string wherever it is being mentioned in the pattern string. We will discuss it in detail in the upcoming session and have examples to make the concept more clear.
CharacterThatNeedsToBeEscaped: Whenever you go for pattern matching, there are certain characters in the demoString that is the original string that you wish to skip while matching. These characters are called as escape characters. By default, the backslash is considered to be an escape character that will be skipped while pattern matching. If you want to consider backslash in pattern matching just specify double backslash as the escape character. You can specify any other character you want to skip in CharacterThatNeedsToBeEscaped parameter. This field is optional and has backslash as default value.
Functions of PostgreSQL Like
When you use LIKE expression to match certain string with the pattern, if the pattern matches then true is returned else false is returned by the like expression. NOT LIKE expression behaves oppositely. It returns true when the string does match the pattern and false when the match is successful. demoString NOT LIKE anyPattern [ESCAPE characterThatNeedsToBeEscaped] is equivalent to NOT (LIKE anyPattern [ESCAPE characterThatNeedsToBeEscaped] ).
The pattern can be any string that you want to compare and can contain underscore and percent signs to help you in matching multiple cases. When no such signs are mentioned in the pattern and are a plain string then LIKE behaves the same as that of equals operator. When underscore is there then that position can be occupied by any other character in the original string to be matched. While the percent sign makes sure that there can be the presence of zero or more characters wherever it is mentioned. Let us study all these cases one by one.
Examples to Implement of PostgreSQL Like
Let us create one example and insert few records in the table to learn how we can use a LIKE expression for pattern matching. Open your PostgreSQL command-line prompt and enter the following command to create a table named educba:
CREATE TABLE educba
(id INTEGER PRIMARY KEY,
Let us insert some values in educba table using the following statement:
Let us create a simple select query statement to retrieve all the records from the educba table. Our query statement will be as follows.
SELECT * FROM educba;
Here, * represents all the columns to be retrieved and firing above query results in the following output:
Example #4 – String Matching a String
Let us take a pattern without any underscore or percent sign in it and match it with LIKE expression.
SELECT technologies FROM educba WHERE technologies LIKE 'jav';
As there is no record with a technologies column having jav value, it returns zero rows.
Let us now fire the following query.
SELECT * FROM educba WHERE technologies LIKE 'java';
There are two records with java named technologies, hence two records are retrieved.
Example #6 – String Matching with _ in a Pattern
Now, we will retrieve all the records that will have underscore for the first position and then the string SQL for technologies column value. For this, we will fire the following query statement –
SELECT * FROM educba WHERE technologies LIKE '_sql';
that gives the following output as there is just one record with technologies value psql that matches the pattern.
Now, we will retrieve the records that have __ two underscores in first and second position and then the string SQL using the following query statement.
SELECT * FROM educba WHERE technologies LIKE '__sql';
Only one record with MySQL technologies value is there that matches the pattern specified by us.
Similarly, we can place underscore any number of times at any position we wish to build up our pattern. When we want to find records having technologies field value such that it contains one character previous and one character after the av substring, we will fire the query statement as follows –
SELECT * FROM educba WHERE technologies LIKE '_av_';
Inserting one more _ in the above pattern gives the following query:
SELECT * FROM educba WHERE technologies LIKE '_av__';
Example #10 – String Matching with % in a Pattern
Suppose, we want to retrieve all the records having string SQL in the ending and having any number of characters before it in the technologies field. Then we can make the use of % sign that checks for zero or more character presence and my query statement will be as follows:
SELECT * FROM educba WHERE technologies LIKE '%sql';
that results in the output containing psql as well as MySQL as there can be any number of characters before SQL string in the technologies field value.
Now, for retrieving records that will contain av in between the string and present anywhere in technologies field value then we will have to mention % before and after av to search for substring av presence in an original string of technologies field value. Y query statement will be as follows
SELECT * FROM educba WHERE technologies LIKE '%av%';
We can perform pattern matching using the LIKE expression in PostgreSQL to retrieve the records that match the particular pattern. Further, we can perform operations based on whether they match the pattern such as splitting or replacing the substring in the original string.
This is a guide to PostgreSQL Like. Here we discuss the Introduction to PostgreSQL Like and its working along with practical examples and different subquery expressions. You can also go through our suggested articles to learn more –