PostgreSQL ABS Function (6 Examples)

Hannah Brooks’ project had a $48,000 budget and came in at $52,350.75. PostgreSQL ABS turns that gap into one positive number: abs(actual - budget) returns 4350.75.

The catch: ABS returns the input type. So abs(-2147483648) fails with “integer out of range”, and timestamps need a workaround.

Six examples follow: variance, tolerance, closest value, date gaps, intervals and @. Tested on PostgreSQL 18.6 for Windows.

PostgreSQL ABS Function Syntax

Here is the syntax:

abs ( numeric_type ) → numeric_type
@ numeric_type → numeric_type
  • numeric_type (required): smallint, integer, bigint, numeric, real or double precision. NULL returns NULL.

Return value: the input’s type, so abs(-5.50) returns 5.50.

Availability: every supported release; no text, money or interval version.

When to Use the ABS Function in PostgreSQL

Use ABS when you need to:

  • report budget or forecast variance
  • keep rows inside a tolerance
  • find the value closest to a target
  • count days early or late

It pairs well with the PostgreSQL ROUND function for long decimals. Run this once; every example uses it:

DROP TABLE IF EXISTS projects;
CREATE TABLE projects (
  project_id int PRIMARY KEY,
  manager    varchar(20),
  budget     numeric(10,2),
  actual     numeric(10,2),
  due_date   date,
  done_date  date
);
INSERT INTO projects VALUES
 (101, 'Hannah Brooks',  48000.00,  52350.75, '2026-03-14', '2026-03-10'),
 (102, 'Tyler Reed',     125000.00, 118420.50, '2026-04-30', '2026-05-06'),
 (103, 'Grace Miller',   76500.00,  76880.00, '2026-05-15', '2026-05-15'),
 (104, 'Marcus Bennett', 31250.00,  29990.25, '2026-06-01', '2026-06-12'),
 (105, 'Olivia Carter',  54000.00,      NULL, '2026-07-20', NULL),
 (106, 'Ethan Parker',   50900.00,  50612.40, '2026-02-27', '2026-02-24');

Example 1: Absolute Difference Between Two Columns

Start with the intro’s job. I want each project’s variance as a positive amount:

\pset null '(null)'
SELECT project_id,
       actual - budget      AS diff,
       abs(actual - budget) AS variance
FROM projects;

Output:

 project_id |   diff   | variance 
------------+----------+----------
        101 |  4350.75 |  4350.75
        102 | -6579.50 |  6579.50
        103 |   380.00 |   380.00
        104 | -1259.75 |  1259.75
        105 |   (null) |   (null)
        106 |  -287.60 |   287.60
(6 rows)
PostgreSQL abs(actual - budget) returning 6579.50 variance for project 102 in VS Code
Diff vs ABS variance.

For project 102, the diff is -6579.50 and the variance is 6579.50. Project 105 has no actual, so it’s NULL.

The subtraction runs first; ABS only drops the sign.

Example 2: Filter Rows Within a Tolerance (WHERE ABS)

Now use it as a filter. The goal is projects within $500 of budget:

SELECT project_id, manager, abs(actual - budget) AS variance
FROM projects
WHERE abs(actual - budget) <= 500;

Output:

 project_id |   manager    | variance 
------------+--------------+----------
        103 | Grace Miller |   380.00
        106 | Ethan Parker |   287.60
(2 rows)

That returns 2 rows: Grace Miller at 380.00 and Ethan Parker at 287.60. I have a separate guide on the PostgreSQL WHERE clause.

Example 3: Sort by Distance to a Target Value

ABS works in ORDER BY too. Say you need the 3 budgets closest to $50,000:

SELECT project_id, budget, abs(budget - 50000) AS distance
FROM projects
ORDER BY abs(budget - 50000)
LIMIT 3;

Output:

 project_id |  budget  | distance 
------------+----------+----------
        106 | 50900.00 |   900.00
        101 | 48000.00 |  2000.00
        105 | 54000.00 |  4000.00
(3 rows)
PostgreSQL ORDER BY abs(budget - 50000) LIMIT 3 returning project 106 at distance 900.00
Closest budget first.

Project 106 comes first at 900.00 away, then 101 and 105. Higher and lower budgets compete fairly; my PostgreSQL ORDER BY guide covers ties.

Example 4: Days Between Two Dates, Early or Late

In PostgreSQL, date - date returns an integer, so ABS accepts it. I want how many days each project missed its due date by:

SELECT project_id,
       done_date - due_date      AS days_diff,
       abs(done_date - due_date) AS days_off
FROM projects
WHERE done_date IS NOT NULL;

Output:

 project_id | days_diff | days_off 
------------+-----------+----------
        101 |        -4 |        4
        102 |         6 |        6
        103 |         0 |        0
        104 |        11 |       11
        106 |        -3 |        3
(5 rows)
PostgreSQL abs(done_date - due_date) returning 11 days off for project 104
date – date is an integer.

Project 104 finished 11 days late; 101 was 4 days early and shows 4.

Change a done_date and run the SELECT again; you should see days_off follow. More in days between two dates in PostgreSQL.

Example 5: Does ABS Work on an Interval?

Say you want the gap between two timestamps. Here’s the catch: timestamp minus timestamp is an interval, and PostgreSQL 18 has no abs(interval):

SELECT abs(timestamp '2026-03-10 09:00'
         - timestamp '2026-03-14 17:30');

Output:

psql:abs-interval-error.sql:2: ERROR:  function abs(interval) does not exist
LINE 1: SELECT abs(timestamp '2026-03-10 09:00'
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
PostgreSQL error function abs(interval) does not exist with the explicit type casts HINT
No abs(interval) in PostgreSQL 18.

Intervals do have a unary minus, so greatest(i, -i) keeps the positive one:

SELECT greatest(i, -i) AS gap
FROM (SELECT timestamp '2026-03-10 09:00'
           - timestamp '2026-03-14 17:30' AS i) AS t;

Output:

       gap       
-----------------
 4 days 08:30:00
(1 row)

This returns 4 days 08:30:00. @ fails on intervals too; timestamp differences has more.

Example 6: The @ Absolute Value Operator in PostgreSQL

You want the @ prefix operator on the variance column:

SELECT @ (actual - budget) AS variance
FROM projects
WHERE project_id = 104;

Output:

 variance 
----------
  1259.75
(1 row)

For project 104, @ (actual - budget) returns 1259.75. Keep the space: @-7 parses as operator @- and fails.

Tips & Common Mistakes

  • abs(-2147483648) gives ERROR: integer out of range. abs(-2147483648::bigint) returns 2147483648.
  • Text fails with ERROR: function abs(text) does not exist; cast with PostgreSQL CAST first.
  • ABS in WHERE skips a plain index. On 200,000 rows, abs(price - 400) <= 0.5 took a Seq Scan (32.7 ms); BETWEEN 399.5 AND 400.5 used the index (0.7 ms, EXPLAIN ANALYZE).

Wrapping Up

In PostgreSQL, abs(a - b) is the call you’ll use most. Copy Example 2 for tolerances, Example 5 for timestamps.

Next step: summarize variance by month with PostgreSQL GROUP BY. See the complete list of PostgreSQL functions and the PostgreSQL 18 math functions page.

Two more PostgreSQL pages worth a look:

Frequently Asked Questions

How do I get a percentage difference with ABS in PostgreSQL?

Divide by the base: round(abs(actual – budget) / budget * 100, 2) returns 9.06 for project 101.

Is ABS the same in PostgreSQL and SQL Server?

Close. SQL Server widens smallint to int and accepts money. Only PostgreSQL has @.

Can an index speed up WHERE abs(actual – budget) <= 500?

Yes, an expression index: CREATE INDEX ON projects ((abs(actual – budget))).