Open In App

SQL Server ABS() Function

Last Updated : 06 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The SQL Server ABS() function is a mathematical function used to return the absolute value of the given numeric expression. This function effectively removes any negative sign from input ensuring that the result is always non-negative. It is commonly used in data analysis and calculations where only the magnitude of the number is relevant. The ABS() function is particularly useful in scenarios involving the financial data, statistical analysis and data normalization.

The SQL Server ABS() function returns the absolute value of a specified number. It changes negative integers to positive integers.

This function takes as an argument any numeric data type or any non-numeric data type that can be implicitly converted to a numeric data type. The value returned by this function is of the same data type as the numeric data type of the argument.

Syntax

The syntax of the ABS() function in SQL Server is:

SELECT ABS(number);

where,

  • number: Specified numeric value whose absolute value going to be returned.

SQL Server ABS() Function Examples

Let's look at some examples of the ABS() function in SQL Server.

Example 1: Getting the absolute value of -1 in SQL Server

This example demonstrates how to use the ABS() function to get the absolute value of -1.

SELECT ABS(-1);

Output:

1

Here, the negative sign is removed, and the function returns the positive equivalent.

Example 2: Getting the absolute value of 0 in SQL Server

This example shows how the ABS() function handles zero.

SELECT ABS(0);

Output:

0

Since the absolute value of zero is zero, the function returns 0.

Example 3: Getting the absolute value 0.7

This example demonstrates how the ABS() function works with decimal values

SELECT ABS(-0.7);

Output:

0.7

The function converts the negative decimal -0.7 to its positive equivalent, 0.7.

Example 4: Using the ABS() Function with a Variable

You can also use the ABS() function with variables in SQL Server. This example shows how:

DECLARE @Parameter_Value INT;
SET @Parameter_Value = 123;
SELECT ABS(@Parameter_Value);

Output:

123

In this case, the negative value of -123 is converted to 123 using the ABS() function.

Important Points About ABS() function

  • The ABS() function is used to return the absolute value of a numeric expression. The absolute value is the positive value of the expression, regardless of its sign.
  • The ABS() function can be used with integer, decimal, float, and money data types.
  • It returns the same data type as the input expression.
  • If the input value is negative, the ABS() function will return the positive value. For example, ABS(-5) will return 5.
  • If the input value is zero, the ABS() function will return 0, as the absolute value of zero is zero.

Conclusion

The ABS() function in SQL Server is a versatile and essential tool for handling numeric data. You are working on financial reports, statistical analysis, or general data processing, and this function ensures that you can always obtain the non-negative value of any number. It can be applied to a wide range of numeric data types and used in various scenarios to improve the accuracy and efficiency of your SQL queries.


Next Article
Article Tags :

Similar Reads