Chapter 6. Functions and Operators

Table of Contents
6.1. Logical Operators
6.2. Comparison Operators
6.3. Mathematical Functions and Operators
6.4. String Functions and Operators
6.5. Binary String Functions and Operators
6.6. Pattern Matching
6.6.1. LIKE
6.6.2. SIMILAR TO and SQL99 Regular Expressions
6.6.3. POSIX Regular Expressions
6.7. Data Type Formatting Functions
6.8. Date/Time Functions and Operators
6.8.1. EXTRACT, date_part
6.8.2. date_trunc
6.8.3. AT TIME ZONE
6.8.4. Current Date/Time
6.9. Geometric Functions and Operators
6.10. Network Address Type Functions
6.11. Sequence-Manipulation Functions
6.12. Conditional Expressions
6.12.1. CASE
6.12.2. COALESCE
6.12.3. NULLIF
6.13. Miscellaneous Functions
6.14. Aggregate Functions
6.15. Subquery Expressions
6.15.1. EXISTS
6.15.2. IN (scalar form)
6.15.3. IN (subquery form)
6.15.4. NOT IN (scalar form)
6.15.5. NOT IN (subquery form)
6.15.6. ANY/SOME
6.15.7. ALL
6.15.8. Row-wise Comparison

PostgreSQL provides a large number of functions and operators for the built-in data types. Users can also define their own functions and operators, as described in the PostgreSQL 7.3.5 Programmer's Guide. The psql commands \df and \do can be used to show the list of all actually available functions and operators, respectively.

If you are concerned about portability then take note that most of the functions and operators described in this chapter, with the exception of the most trivial arithmetic and comparison operators and some explicitly marked functions, are not specified by the SQL standard. Some of this extended functionality is present in other SQL implementations, and in many cases this functionality is compatible and consistent between various products.

6.1. Logical Operators

The usual logical operators are available:

AND
OR
NOT

SQL uses a three-valued Boolean logic where the null value represents "unknown". Observe the following truth tables:

aba AND ba OR b
TRUETRUETRUETRUE
TRUEFALSEFALSETRUE
TRUENULLNULLTRUE
FALSEFALSEFALSEFALSE
FALSENULLFALSENULL
NULLNULLNULLNULL

aNOT a
TRUEFALSE
FALSETRUE
NULLNULL