Difference between revisions of "ABS"

From SQLZOO
Jump to: navigation, search
(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...")
 
 
(12 intermediate revisions by one user not shown)
Line 1: Line 1:
<h3>ABS</h3>
+
<table align='right' border='1'>
 +
<caption>Compatibility</caption>
 +
<tr><th colspan='3'>ABS(f)</th></tr>
 +
<tr><td align='center'>'''Engine'''</td><td align='center'>'''OK'''</td><td align='center'>'''Alternative'''</td></tr>
 +
<tr><td align='left'>ingres</td><td>Yes</td><td></td></tr>
 +
<tr><td align='left'>mysql</td><td>Yes</td><td></td></tr>
 +
<tr><td align='left'>oracle</td><td>Yes</td><td></td></tr>
 +
<tr><td align='left'>postgres</td><td>Yes</td><td></td></tr>
 +
<tr><td align='left'>sqlserver</td><td>Yes</td><td></td></tr>
 +
</table>
 +
<h1>ABS</h1>
 
<p>ABS returns the absolute value. The output is positive even if the input is negative:</p>
 
<p>ABS returns the absolute value. The output is positive even if the input is negative:</p>
  ABS(x) = x if x>=0
+
<pre style='width:40ex'>ABS(x) = x if x>=0
  ABS(x) = -x if x<0
+
ABS(x) = -x if x<0</pre>
  
 
<div class='qu'>
 
<div class='qu'>
Line 11: Line 21:
 
  WHERE ABS(area-70000)<500
 
  WHERE ABS(area-70000)<500
 
</source>
 
</source>
 +
</div>
  
<source lang='sql' class='ans'>
+
<p>See also</p>
SELECT name, area FROM bbc
+
<ul>
WHERE ABS(area-70000)<500
+
  <li>[[CASE |CASE statement]]</li>
</source>
+
</ul>
</div>
+
 
 +
{{Languages}}

Latest revision as of 13:35, 6 September 2012

Compatibility
ABS(f)
EngineOKAlternative
ingresYes
mysqlYes
oracleYes
postgresYes
sqlserverYes

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

See also

Language: English  • Deutsch
Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense