Saturday, January 2, 2016

SQL - ABS() Function

In SQL, a built-in function is a piece for programming that takes zero or more inputs and returns a value. 
The ABS() function is a mathematical function means they are based on input values that are provided as arguments, and return a numeric value in SQL Server. The ABS() function is used to ignore a negative number or value. This function returns a positive value if any value is not absolute or positive. The Numeric function abs always returns the absolute (positive) value of number.

Syntax:-
ABS (Numeric Expression)
Arguments: This function uses the following parameters.
Numeric Expression: is an expression of the exact numeric or approximate numeric data type category, except for the bit data type.
Return Types: Returns the same type as Numeric Expression.
Important points to remember:
1. The ABS function can produce an overflow error when the absolute value of a number is greater than the largest number that can be represented by the specified data type.
2. ABS function has remove the negative portion of numeric expression.

---- declare local variables
declare @intVar1 int=-1,
@intVar2 int=0,
@intVar3 int=3
---- Output with ABS also
Select Var1=@intVar1,
Var2=@intVar2,
Var3=@intVar3,
ABS_Var1 =ABS(@intVar1),
ABS_Var2 =ABS(@intVar2),
ABS_Var3 =ABS(@intVar3),
ABS_Sum = ABS(SUM(@intVar1+@intVar2+@intVar3))
----- Table Variable output
Var1
Var2
Var3
ABS_Var1
ABS_Var2
ABS_Var3
ABS_Sum
-1
0
3
1
0
3
2

For example, the int data type can hold only values that range from 2,147,483,648 to 2,147,483,647. Computing the absolute value for the signed integer 2,147,483,648 causes an overflow error because its absolute value is greater than the positive range for the int data type.
Conclusion: ABS Function is used to return the positive value of any numeric value and also allows us to find the absolute values of a column values.

No comments:

Post a Comment

Popular Posts