PostgreSQL

Postgres Absolute Value

In PostgreSQL, the absolute function is a mathematical function that returns a given number’s absolute (positive) value.

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:

SELECT ABS(expression);

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:

select abs(10);

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:

select abs(-10);

This should return a positive integer as follows:

abs

-----

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:

select abs(10.34);

Output:

abs

-------

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:

select abs(-10.34);

This should return the positive equivalent as follows:

abs

-------

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:

select abs(3.14159 * 7 * 7) as absolute_area;

This should return the following output:

absolute_area
---------------
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.

CREATE TABLE numbers (

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:

SELECT ABS(value) FROM numbers;

The resulting values are as follows:

select id, abs(value) from numbers n;

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list