# Difference between revisions of "ABS"

From SQLZOO

(Created page with "<h3>ABS</h3> <p>ABS returns the absolute value. The output is positive even if the input is negative:</p> ABS(x) = x if x>=0 ABS(x) = -x if x<0 <div class='qu'> ABS can b...") |
|||

Line 17: | Line 17: | ||

</source> | </source> | ||

</div> | </div> | ||

+ | |||

+ | <ul> | ||

+ | <li>[[CASE statement]]</li> | ||

+ | </ul> |

## Revision as of 15:11, 12 July 2012

### ABS

ABS returns the absolute value. The output is positive even if the input is negative:

ABS(x) = x if x>=0 ABS(x) = -x if x<0

ABS can be useful for testing values that are "close". For example this query shows each country that has area that is roughly 70 thousand. The value 70000 is the target value, 500 is the "tolerance" so the test ABS(area-70000)<500 tests that the area is between 70000-500 and 70000+500. That is 69500<area<70500

SELECT name, area FROM bbc WHERE ABS(area-70000)<500

SELECT name, area FROM bbc WHERE ABS(area-70000)<500