PostgreSQL 18 reference · Math Functions
PostgreSQL ABS() Function
Return the absolute value of a number, so abs(-7) is 7. The result has the same type as the input.
PostgreSQL 18
Syntax
abs(x)
Parameters
| Parameter | Description |
|---|---|
x |
A number (integer, numeric or double precision). |
Return value
A number of the input type.
Examples
SELECT abs(-7) AS a, abs(-3.50) AS b;
Output
a | b ---+------ 7 | 3.50 (1 row)
Run this on your computer — see “Get Started” for setup.
SELECT name, salary, abs(salary - 120000) AS distance
FROM employees
ORDER BY distance
LIMIT 3;
Output
name | salary | distance --------------+--------+---------- Priya Nair | 115000 | 5000 Lucas Martin | 112000 | 8000 Jonas Berg | 140000 | 20000 (3 rows)
Run this on your computer — see “Get Started” for setup.
In other languages
ABS()· SQLite
Related
ROUND()· Round a number to a given number of decimal places (0 by default). numeric rounds halves away from zero.CEIL()· Round a number up to the nearest integer: ceil(2.1) is 3 and ceil(-2.1) is -2. ceiling() is a synonym.- SQL Aggregate Functions · tutorial