Difference between revisions of "MOD"
From SQLZOO
| Line 20: | Line 20: | ||
<div class='qu'> | <div class='qu'> | ||
With a <code>GROUP BY region</code> statement each region shows up just once. The SUM column gives the total for each region. | With a <code>GROUP BY region</code> statement each region shows up just once. The SUM column gives the total for each region. | ||
| − | <source lang='sql' class='def'> | + | <source lang='sql' class='def e-sqlserver'> |
| − | SELECT | + | SELECT yr % 10, |
yr, city | yr, city | ||
FROM games | FROM games | ||
</source> | </source> | ||
| − | <source lang='sql' class='def | + | <source lang='sql' class='def'> |
| − | SELECT yr | + | SELECT MOD(yr,10), |
yr, city | yr, city | ||
FROM games | FROM games | ||
</source> | </source> | ||
| + | |||
</div> | </div> | ||
<p>See also</p> | <p>See also</p> | ||
Revision as of 09:08, 13 July 2012
| MOD(a,b) | ||
|---|---|---|
| Engine | OK | Alternative |
| ingres | Yes | |
| mysql | Yes | a % b |
| oracle | Yes | |
| postgres | Yes | a % b |
| sqlserver | No | a % 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
With a GROUP BY region statement each region shows up just once. The SUM column gives the total for each region.
SELECT yr % 10, yr, city FROM games
SELECT MOD(yr,10), yr, city FROM games
See also