SQLite ABS() Function
The SQLite ABS() function returns the absolute value of a given number.
ABS(X) returns NULL if X is NULL. ABS(X) returns 0.0 if X is a string or blob that cannot be converted to a numeric value. If X is the integer -9223372036854775808 then ABS(X) throws an integer overflow error since there is no equivalent positive 64-bit two complement value.
Syntax
ABS(number)
Parameters
number |
Required. Specify the number to convert to an absolute value. |
Return Value
Returns the absolute value of a given number.
Example 1:
The example below shows the usage of ABS() function.
SELECT ABS(15); Result: 15 SELECT ABS(-15); Result: 15 SELECT ABS(25.23); Result: 25.23 SELECT ABS(-25.23); Result: 25.23 SELECT ABS(0); Result: 0 SELECT ABS(10 * -2); Result: 20 SELECT ABS(NULL); Result: NULL
Example 2:
Consider a database table called Sample with the following records:
Data | x |
---|---|
Data 1 | -10 |
Data 2 | -5 |
Data 3 | 0 |
Data 4 | 5 |
Data 5 | 10 |
The statement given below can be used to calculate the absolute value of column x.
SELECT *, ABS(x) AS ABS_Value FROM Sample;
This will produce the result as shown below:
Data | x | ABS_Value |
---|---|---|
Data 1 | -10 | 10 |
Data 2 | -5 | 5 |
Data 3 | 0 | 0 |
Data 4 | 5 | 5 |
Data 5 | 10 | 10 |
❮ SQLite Functions