SQL Server ABS() Function
Last Updated :
06 Oct, 2024
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.
Similar Reads
AVG() Function in SQL Server
The AVG() function in SQL Server is an essential aggregate function used to compute the average value of a numeric column. It works by summing all non-NULL values in the specified column and then dividing the total by the number of these values. In this article, We will learn about AVG() Function in
3 min read
ATN2() Function in SQL Server
In this article, we are going to cover the ATN2()function in which we will see how we can get the arc tangent of two given numbers. Let's discuss one by one. // here val1 and val2 are input. Input : ATN2(val1, val2) Output : Arc tangent result. ATN2() : It is the function that returns the arc tangen
1 min read
SQL Server LEN() Function
SQL SERVER LEN() function calculates the number of characters of an input string, excluding the trailing spaces. LEN() Function in SQL ServerThe LEN function in the SQL Server fetches the number of characters in a string. It counts the preceding spaces but not the trailing spaces. For eg, 'SQL SERVE
2 min read
SQL Server DATEDIFF() Function
The DATEDIFF() function in SQL Server is a powerful tool used to calculate the difference between two dates or times. It returns an integer representing the number of date or time boundaries crossed between the specified dates, based on the specified date. This function is essential for tasks that i
3 min read
SQL Server TRIM() Function
The SQL Server TRIM() function omits the space character or additional stated characters from the beginning or the end of a specified string. TRIM in SQL ServerThe TRIM function in SQL Server is used to remove leading and trailing spaces from a string. Itâs particularly useful for manipulating and c
2 min read
SQL Server SPACE() function
The SQL Server SPACE() function is a handy tool for generating a string of space characters. It is particularly useful for formatting and padding data within SQL queries. By specifying the number of spaces, users can ensure consistent spacing in output results, which can be critical for aligning tex
3 min read
SQL Server ROUND() Function
The SQL Server ROUND() function rounds off a specified number to a decimal place. If the length is negative and larger than the number of digits before the decimal point, ROUND returns 0. ROUND in SQL ServerThe ROUND() function in SQL Server rounds off a number to a specified decimal place. It accep
2 min read
SQL Server POWER() Function
The POWER() function in SQL Server is a mathematical function that computes the result of raising a number (the base) to the power of another number (the exponent). It is a versatile function used for various calculations, such as squaring a number, computing roots or applying exponential growth in
3 min read
PLSQL | ABS Function
The PLSQL ABS function is used for returning the absolute value of a number. Absolute value is used for depicting the distance of a number on the number line from 0. The direction of the number from zero is not considered since the absolute value of a number is never negative. The ABS in PLSQL funct
2 min read
MIN() Function in SQL Server
MIN() : This function in SQL Server is used to find the value that is minimum in the group of values stated. Features : This function is used to find the minimum value.This function comes under Numeric Functions.This function accepts only one parameter namely expression. Syntax : MIN(expression) Par
2 min read