In this tutorial, we will learn how to work with the absolute function in PostgreSQL including its syntax and examples.

## Requirements:

In this tutorial, we assume that you have a basic understanding of PostgreSQL and running SQL queries. We also believe that you have an installed PostgreSQL server on your system.

You can create a test database to run the provided examples for testing purposes. However, kindly avoid using the real-world databases for test commands unless you want to implement the functions in production.

## PostgreSQL Absolute Function()

To calculate the absolute of an input number in PostgreSQL, we use the abs() function. The syntax is as follows:

The input expression can be a numerical value, a numeric column, or an arithmetic expression that returns a numeric value.

The function returns a data type that is similar to the input argument.

## Examples:

Let us explore some basic examples on how to work with this function.

### Example 1: Calculate the Absolute Value of a Positive Integer

The following example demonstrates how to use the abs() function to calculate the absolute value of a given numerical input:

Output:

abs

-----

10

### Example 2: Calculate the Absolute Value of a Negative Integer

We can also determine the absolute value of a negative integer as shown in the following example:

This should return a positive integer as follows:

-----

10

(1 row)

### Example 3: Calculate the Absolute Value of a Positive Float

The function also allows us to specify a floating point value as demonstrated in the following example:

Output:

-------

10.34

(1 row)

### Example 4: Calculate the Absolute Value of the Negative Floating Point

We can also do a similar operation on a negative floating point value as demonstrated in the following example:

This should return the positive equivalent as follows:

-------

10.34

(1 row)

### Example 5: Calculate the Absolute Value of an Expression

We can also determine the absolute value of a given expression, provided that the expression evaluates to a numerical value.

For example:

This should return the following output:

---------------

153.93791

(1 row)

### Example 6: Calculate the Absolute Value of a Numerical Column

We can also provide a numerical column to the abs() function.

Consider a table called “numbers” with a column named “values” which contains both positive and negative floating point values.

id SERIAL PRIMARY KEY,

value FLOAT

);

INSERT INTO numbers (value) VALUES (10.23), (-5.56), (1.99), (-8.65), (15.22);

To determine the absolute equivalent of the values in the numbers table, we can run the query as follows:

The resulting values are as follows:

The resulting table is as follows:

## Conclusion

We explored how to work with the abs() function in PostgreSQL to determine the absolute values of a given numerical input.

Ensuring that the expression that you pass to the abs() function evaluates to a numerical value is good. The function fails with an exception if the input is not a number.