Difference between revisions of "MOD"

From SQLZOO
Jump to: navigation, search
 
(6 intermediate revisions by one user not shown)
Line 4: Line 4:
 
<tr><td align='center'>'''Engine'''</td><td align='center'>'''OK'''</td><td align='center'>'''Alternative'''</td></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'>ingres</td><td>Yes</td><td></td></tr>
<tr><td align='left'>mysql</td><td>Yes</td><td>[[a % b]]</td></tr>
+
<tr><td align='left'>mysql</td><td>Yes</td><td>[[%25_MODULO |a % b]]</td></tr>
 
<tr><td align='left'>oracle</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>[[a % b]]</td></tr>
+
<tr><td align='left'>postgres</td><td>Yes</td><td>[[%25_MODULO |a % b]]</td></tr>
<tr><td align='left'>sqlserver</td><td>No</td><td>[[a % b]]</td></tr>
+
<tr><td align='left'>sqlserver</td><td>No</td><td>[[%25_MODULO |a % b]]</td></tr>
 
</table>
 
</table>
<h3>MOD</h3>
+
<h1>MOD</h1>
 
<p>MOD(a,b) returns the remainder when a is divied by b</p>
 
<p>MOD(a,b) returns the remainder when a is divied by b</p>
 
<p>If you use MOD(a, 2) you get 0 for even numbers and 1 for odd numbers.</p>
 
<p>If you use MOD(a, 2) you get 0 for even numbers and 1 for odd numbers.</p>
Line 19: Line 19:
  
 
<div class='ht'>
 
<div class='ht'>
With a <code>GROUP BY region</code> statement each region shows up just once. The SUM column gives the total for each region.  
+
In this example you get the final digit year of the games.  
<source lang='sql' class='def e-sqlserver'>SELECT yr%10, yr, city
+
<source lang='sql' class='def e-sqlserver'>SELECT yr % 10, yr, city
 
FROM games
 
FROM games
 +
</source>
 +
<source lang='sql' class='def e-oracle'>SELECT MOD(yr,10),
 +
      yr, city
 +
  FROM gisq.games
 
</source>
 
</source>
 
<source lang='sql' class='def'>SELECT MOD(yr,10),
 
<source lang='sql' class='def'>SELECT MOD(yr,10),
Line 29: Line 33:
 
<p>See also</p>
 
<p>See also</p>
 
<ul>
 
<ul>
   <li>[[FLOOR function]]</li>
+
   <li>[[FLOOR |FLOOR function]]</li>
   <li>[[CEIL function]]</li>
+
   <li>[[CEIL |CEIL function]]</li>
 
</ul>
 
</ul>
 +
 +
{{Languages}}

Latest revision as of 12:09, 2 September 2012

Compatibility
MOD(a,b)
EngineOKAlternative
ingresYes
mysqlYesa % b
oracleYes
postgresYesa % b
sqlserverNoa % b

MOD

MOD(a,b) returns the remainder when a is divied by b

If you use MOD(a, 2) you get 0 for even numbers and 1 for odd numbers.

If you use MOD(a, 10) you get the last digit of the number a.

 MOD(27,2) ->  1
 MOD(27,10) ->  7

In this example you get the final digit year of the games.

SELECT yr % 10, yr, city
FROM games
SELECT MOD(yr,10),
       yr, city
  FROM gisq.games
SELECT MOD(yr,10),
       yr, city
  FROM games

See also

Language: English  • Deutsch
Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense