Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PostgreSQL LIKE Clause

PostgreSQL LIKE Clause

In a PostgreSQL database, if we want to retrieve data containing certain characters, we can use the LIKE LIKE clause.

In the LIKE clause, wildcards are typically used together; a wildcard represents any character. In PostgreSQL, there are mainly the following two wildcards:

If neither of the above two wildcards is used, the LIKE clause gives the same result as the equals sign =.

Syntax

The following is the general syntax for using the LIKE clause with the percent sign % and the underscore _ to retrieve data from a database:

SELECT FROM table_name WHERE column LIKE 'XXXX%';

或者

SELECT FROM table_name WHERE column LIKE '%XXXX%';

或者

SELECT FROM table_name WHERE column LIKE 'XXXX_';

或者

SELECT FROM table_name WHERE column LIKE '_XXXX';

或者

SELECT FROM table_name WHERE column LIKE '_XXXX_';

You can specify any condition in the WHERE clause.

You can use AND or OR to specify one or more conditions.

XXXX can be any number or character.

Example

The following examples in LIKE statements demonstrate some differences between % and _:

Example Description
WHERE SALARY::text LIKE '200%' Find data in the SALARY field that starts with 200.
WHERE SALARY::text LIKE '%200%' Find data in the SALARY field that contains the characters 200.
WHERE SALARY::text LIKE '_00%' Find data in the SALARY field that has 00 at the second and third positions.
WHERE SALARY::text LIKE '2_%_%' Find data in the SALARY field that starts with 2 and has a character length greater than 3.
WHERE SALARY::text LIKE '%2' Find data in the SALARY field that ends with 2
WHERE SALARY::text LIKE '_2%3' Find data in the SALARY field where 2 is in the second position and the value ends with 3
WHERE SALARY::text LIKE '2___3' Find data in the SALARY field that starts with 2, ends with 3, and is 5 digits long

In PostgreSQL, the LIKE clause can only be used to compare characters, so in the examples above, we need to convert the integer data type to a string data type.

Create the COMPANY table (Download the COMPANY SQL file ), with the following data:

runoobdb# select * from COMPANY;

 id | name  | age | address   | salary

----+-------+-----+-----------+--------

  1 | Paul  |  32 | California|  20000

  2 | Allen |  25 | Texas     |  15000

  3 | Teddy |  23 | Norway    |  20000

  4 | Mark  |  25 | Rich-Mond |  65000

  5 | David |  27 | Texas     |  85000

  6 | Kim   |  22 | South-Hall|  45000

  7 | James |  24 | Houston   |  10000

(7 rows)

The following example will find data where AGE starts with 2:

runoobdb=# SELECT * FROM COMPANY WHERE AGE::text LIKE '2%';

The following result is obtained:

id | name  | age | address     | salary

----+-------+-----+-------------+--------

  2 | Allen |  25 | Texas       |  15000

  3 | Teddy |  23 | Norway      |  20000

  4 | Mark  |  25 | Rich-Mond   |  65000

  5 | David |  27 | Texas       |  85000

  6 | Kim   |  22 | South-Hall  |  45000

  7 | James |  24 | Houston     |  10000

  8 | Paul  |  24 | Houston     |  20000

(7 rows)

The following example will find data whose address field contains the - character:

runoobdb=# SELECT * FROM COMPANY WHERE ADDRESS  LIKE '%-%';

The result is as follows:

id | name | age |                      address              | salary

----+------+-----+-------------------------------------------+--------

  4 | Mark |  25 | Rich-Mond                                 |  65000

  6 | Kim  |  22 | South-Hall                                |  45000

(2 rows)