5. Functions, Conditional Expressions and Operators
Description
PostgreSQL ships with a large library of built-in functions and operators. This lecture covers three groups: mathematical functions, string functions and conditional expressions.
Part I: Mathematical Functions and Operators
| Function | Purpose | Example |
|---|---|---|
ABS(x) | Absolute value | ABS(-5) → 5 |
CEIL(x) / FLOOR(x) | Round up / down to an integer | CEIL(4.2) → 5 |
ROUND(x, n) | Round to n decimal places | ROUND(3.14159, 2) → 3.14 |
TRUNC(x, n) | Truncate to n decimal places | TRUNC(3.789, 1) → 3.7 |
MOD(x, y) / x % y | Remainder | MOD(10, 3) → 1 |
POWER(x, y) | x raised to y | POWER(2, 3) → 8 |
SQRT(x) | Square root | SQRT(16) → 4 |
EXP(x), LN(x), LOG(x) | Exponential and logarithms | |
SIGN(x) | −1, 0 or 1 |
- Random functions:
RANDOM()returns a value between 0 and 1. - Trigonometric functions:
SIN,COS,TAN,ASIN,ACOS,ATAN,DEGREES,RADIANS,PI().
Part II: String Functions and Operators
| Function | Purpose |
|---|---|
a || b | Concatenate strings |
FORMAT(format_string [, args...]) | Format arguments based on a format string |
LOWER(s), UPPER(s), INITCAP(s) | Change letter case |
LEFT(s, n) | First n characters |
RIGHT(s, n) | Last n characters |
LTRIM(s), RTRIM(s), BTRIM(s) | Remove characters (spaces by default) from the start, the end, or both |
POSITION(substring IN string) | Location of a substring |
REPLACE(source, old_text, new_text) | Replace all occurrences of a string |
SUBSTRING(string, start_position, length) | Part of a string |
LENGTH(s) | Number of characters |
SELECT FORMAT('Hello, %s!', 'PostgreSQL'); -- Hello, PostgreSQL!
SELECT UPPER('database'), INITCAP('hello world'); -- DATABASE, Hello World
SELECT LEFT('PostgreSQL', 4), RIGHT('PostgreSQL', 3); -- Post, SQL
SELECT POSITION('SQL' IN 'PostgreSQL'); -- 8
SELECT REPLACE('2024-01-15', '-', '/'); -- 2024/01/15
SELECT SUBSTRING('PostgreSQL', 1, 4); -- Post
SELECT BTRIM(' padded '); -- padded
Part III: Conditional Expressions
Conditional expressions run different logic depending on conditions. The most used are CASE, COALESCE, NULLIF, GREATEST and LEAST.
CASE
A generic conditional expression, similar to if/else in programming languages.
SELECT title,
CASE
WHEN rental_rate < 1 THEN 'Cheap'
WHEN rental_rate < 3 THEN 'Standard'
ELSE 'Premium'
END AS price_category
FROM film;
COALESCE
Returns the first non-NULL argument. NULL is returned only if all arguments are NULL.
SELECT COALESCE(phone, email, 'no contact') FROM customer;
NULLIF
Returns NULL if value1 = value2; otherwise returns value1. This is the inverse of the COALESCE pattern and is useful for avoiding division by zero.
SELECT total / NULLIF(count, 0) FROM stats;
GREATEST and LEAST
Select the largest or smallest value from a list of any number of expressions.
SELECT GREATEST(1, 5, 3), LEAST(1, 5, 3); -- 5, 1
Conclusion
Built-in math, string and conditional tools let you compute, reformat and branch inside a query without any application code.
All rights remain with the original author (Algo-holics). Publishing here does not grant a license to copy or reuse this text. Copyright & license. If you noticed a mistake or copyright issues, please contact: lebroskiofficial@proton.me