ABS() Examples in SQL Server

InĀ SQL Server, the ABS() function returns the absolute value of a specified value.

You provide the value as an argument. The return value is of the same type as the argument.

An absolute value is the distance of a number on the number line from 0 without considering which direction from zero the number lies. Therefore, the absolute value of a number is always a non-negative value (i.e. it’s never negative).

Syntax

Here’s the syntax of this function:

ABS ( numeric_expression ) 

Where numeric_expression is the value for which you’d like the absolute value returned. It is an expression of the exact numeric or approximate numeric data type category.

Example 1 – Basic Example

Here’s a basic example to demonstrate what ABS() returns for a given number.

SELECT ABS(9) Result;

Result:

+----------+
| Result   |
|----------|
| 9        |
+----------+

In this case, our argument is a positive number and so the same number is returned.

Example 2 – Negative Numbers

Here’s what happens if we pass in a negative value.

SELECT ABS(-9) Result;

Result:

+----------+
| Result   |
|----------|
| 9        |
+----------+

As you can see, the result is a non-negative value (even though we passed in a negative value). This is the absolute value of the argument.

Example 3 – Expressions

You can return the absolute value of expressions, such as the following:

SELECT ABS(9+5) Result;

Result:

+----------+
| Result   |
|----------|
| 14       |
+----------+

Another example:

SELECT ABS(-9+5) Result;

Result:

+----------+
| Result   |
|----------|
| 4        |
+----------+

This is a different result than we’d get if we simply returned the expression itself (without using the ABS() function). Here’s what that would return:

SELECT -9+5 Result;

Result:

+----------+
| Result   |
|----------|
| -4       |
+----------+