<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="date-calculations">
      </a>
      5.3.4.5 Date Calculations
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045325190144">
  </a>
  <a class="indexterm" name="idm46045325189072">
  </a>
  <a class="indexterm" name="idm46045325187584">
  </a>
  <a class="indexterm" name="idm46045325186096">
  </a>
  <p>
   MySQL provides several functions that you can use to perform
          calculations on dates, for example, to calculate ages or
          extract parts of dates.
  </p>
  <p>
   To determine how many years old each of your pets is, use the
   <a class="link" href="date-and-time-functions.html#function_timestampdiff">
    <code class="literal">
     TIMESTAMPDIFF()
    </code>
   </a>
   function. Its
          arguments are the unit in which you want the result expressed,
          and the two dates for which to take the difference. The
          following query shows, for each pet, the birth date, the
          current date, and the age in years. An
   <span class="emphasis">
    <em>
     alias
    </em>
   </span>
   (
   <code class="literal">
    age
   </code>
   ) is used to
          make the final output column label more meaningful.
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa26924748"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token keyword">name</span><span class="token punctuation">,</span> birth<span class="token punctuation">,</span> <span class="token function">CURDATE</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token function">TIMESTAMPDIFF</span><span class="token punctuation">(</span><span class="token datatype">YEAR</span><span class="token punctuation">,</span>birth<span class="token punctuation">,</span><span class="token function">CURDATE</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> age
       <span class="token keyword">FROM</span> pet<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> name     <span class="token punctuation">|</span> birth      <span class="token punctuation">|</span> CURDATE()  <span class="token punctuation">|</span> age  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> Fluffy   <span class="token punctuation">|</span> 1993<span class="token punctuation">-</span>02<span class="token punctuation">-</span>04 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>   10 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Claws    <span class="token punctuation">|</span> 1994<span class="token punctuation">-</span>03<span class="token punctuation">-</span>17 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>    9 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Buffy    <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>05<span class="token punctuation">-</span>13 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>   14 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Fang     <span class="token punctuation">|</span> 1990<span class="token punctuation">-</span>08<span class="token punctuation">-</span>27 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>   12 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Bowser   <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>08<span class="token punctuation">-</span>31 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>   13 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Chirpy   <span class="token punctuation">|</span> 1998<span class="token punctuation">-</span>09<span class="token punctuation">-</span>11 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>    4 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Whistler <span class="token punctuation">|</span> 1997<span class="token punctuation">-</span>12<span class="token punctuation">-</span>09 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>    5 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Slim     <span class="token punctuation">|</span> 1996<span class="token punctuation">-</span>04<span class="token punctuation">-</span>29 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>    7 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Puffball <span class="token punctuation">|</span> 1999<span class="token punctuation">-</span>03<span class="token punctuation">-</span>30 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>    4 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
  </div>
  <p>
   The query works, but the result could be scanned more easily
          if the rows were presented in some order. This can be done by
          adding an
   <code class="literal">
    ORDER BY name
   </code>
   clause to sort the
          output by name:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa10088138"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token keyword">name</span><span class="token punctuation">,</span> birth<span class="token punctuation">,</span> <span class="token function">CURDATE</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token function">TIMESTAMPDIFF</span><span class="token punctuation">(</span><span class="token datatype">YEAR</span><span class="token punctuation">,</span>birth<span class="token punctuation">,</span><span class="token function">CURDATE</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> age
       <span class="token keyword">FROM</span> pet <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> <span class="token keyword">name</span><span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> name     <span class="token punctuation">|</span> birth      <span class="token punctuation">|</span> CURDATE()  <span class="token punctuation">|</span> age  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> Bowser   <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>08<span class="token punctuation">-</span>31 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>   13 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Buffy    <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>05<span class="token punctuation">-</span>13 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>   14 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Chirpy   <span class="token punctuation">|</span> 1998<span class="token punctuation">-</span>09<span class="token punctuation">-</span>11 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>    4 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Claws    <span class="token punctuation">|</span> 1994<span class="token punctuation">-</span>03<span class="token punctuation">-</span>17 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>    9 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Fang     <span class="token punctuation">|</span> 1990<span class="token punctuation">-</span>08<span class="token punctuation">-</span>27 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>   12 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Fluffy   <span class="token punctuation">|</span> 1993<span class="token punctuation">-</span>02<span class="token punctuation">-</span>04 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>   10 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Puffball <span class="token punctuation">|</span> 1999<span class="token punctuation">-</span>03<span class="token punctuation">-</span>30 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>    4 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Slim     <span class="token punctuation">|</span> 1996<span class="token punctuation">-</span>04<span class="token punctuation">-</span>29 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>    7 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Whistler <span class="token punctuation">|</span> 1997<span class="token punctuation">-</span>12<span class="token punctuation">-</span>09 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>    5 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
  </div>
  <p>
   To sort the output by
   <code class="literal">
    age
   </code>
   rather than
   <code class="literal">
    name
   </code>
   , just use a different
   <code class="literal">
    ORDER
          BY
   </code>
   clause:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa27967240"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token keyword">name</span><span class="token punctuation">,</span> birth<span class="token punctuation">,</span> <span class="token function">CURDATE</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
       <span class="token function">TIMESTAMPDIFF</span><span class="token punctuation">(</span><span class="token datatype">YEAR</span><span class="token punctuation">,</span>birth<span class="token punctuation">,</span><span class="token function">CURDATE</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> age
       <span class="token keyword">FROM</span> pet <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> age<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> name     <span class="token punctuation">|</span> birth      <span class="token punctuation">|</span> CURDATE()  <span class="token punctuation">|</span> age  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> Chirpy   <span class="token punctuation">|</span> 1998<span class="token punctuation">-</span>09<span class="token punctuation">-</span>11 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>    4 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Puffball <span class="token punctuation">|</span> 1999<span class="token punctuation">-</span>03<span class="token punctuation">-</span>30 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>    4 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Whistler <span class="token punctuation">|</span> 1997<span class="token punctuation">-</span>12<span class="token punctuation">-</span>09 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>    5 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Slim     <span class="token punctuation">|</span> 1996<span class="token punctuation">-</span>04<span class="token punctuation">-</span>29 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>    7 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Claws    <span class="token punctuation">|</span> 1994<span class="token punctuation">-</span>03<span class="token punctuation">-</span>17 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>    9 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Fluffy   <span class="token punctuation">|</span> 1993<span class="token punctuation">-</span>02<span class="token punctuation">-</span>04 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>   10 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Fang     <span class="token punctuation">|</span> 1990<span class="token punctuation">-</span>08<span class="token punctuation">-</span>27 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>   12 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Bowser   <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>08<span class="token punctuation">-</span>31 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>   13 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Buffy    <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>05<span class="token punctuation">-</span>13 <span class="token punctuation">|</span> 2003<span class="token punctuation">-</span>08<span class="token punctuation">-</span>19 <span class="token punctuation">|</span>   14 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
  </div>
  <p>
   A similar query can be used to determine age at death for
          animals that have died. You determine which animals these are
          by checking whether the
   <code class="literal">
    death
   </code>
   value is
   <code class="literal">
    NULL
   </code>
   . Then, for those with
          non-
   <code class="literal">
    NULL
   </code>
   values, compute the difference
          between the
   <code class="literal">
    death
   </code>
   and
   <code class="literal">
    birth
   </code>
   values:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa52403723"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token keyword">name</span><span class="token punctuation">,</span> birth<span class="token punctuation">,</span> death<span class="token punctuation">,</span>
       <span class="token function">TIMESTAMPDIFF</span><span class="token punctuation">(</span><span class="token datatype">YEAR</span><span class="token punctuation">,</span>birth<span class="token punctuation">,</span>death<span class="token punctuation">)</span> <span class="token keyword">AS</span> age
       <span class="token keyword">FROM</span> pet <span class="token keyword">WHERE</span> death <span class="token operator">IS</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> age<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> name   <span class="token punctuation">|</span> birth      <span class="token punctuation">|</span> death      <span class="token punctuation">|</span> age  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> Bowser <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>08<span class="token punctuation">-</span>31 <span class="token punctuation">|</span> 1995<span class="token punctuation">-</span>07<span class="token punctuation">-</span>29 <span class="token punctuation">|</span>    5 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
  </div>
  <p>
   The query uses
   <code class="literal">
    death IS NOT NULL
   </code>
   rather
          than
   <code class="literal">
    death &lt;&gt; NULL
   </code>
   because
   <code class="literal">
    NULL
   </code>
   is a special value that cannot be
          compared using the usual comparison operators. This is
          discussed later. See
   <a class="xref" href="working-with-null.html" title="5.3.4.6 Working with NULL Values">
    Section 5.3.4.6, “Working with NULL Values”
   </a>
   .
  </p>
  <p>
   What if you want to know which animals have birthdays next
          month? For this type of calculation, year and day are
          irrelevant; you simply want to extract the month part of the
   <code class="literal">
    birth
   </code>
   column. MySQL provides several
          functions for extracting parts of dates, such as
   <a class="link" href="date-and-time-functions.html#function_year">
    <code class="literal">
     YEAR()
    </code>
   </a>
   ,
   <a class="link" href="date-and-time-functions.html#function_month">
    <code class="literal">
     MONTH()
    </code>
   </a>
   , and
   <a class="link" href="date-and-time-functions.html#function_dayofmonth">
    <code class="literal">
     DAYOFMONTH()
    </code>
   </a>
   .
   <a class="link" href="date-and-time-functions.html#function_month">
    <code class="literal">
     MONTH()
    </code>
   </a>
   is the appropriate
          function here. To see how it works, run a simple query that
          displays the value of both
   <code class="literal">
    birth
   </code>
   and
   <a class="link" href="date-and-time-functions.html#function_month">
    <code class="literal">
     MONTH(birth)
    </code>
   </a>
   :
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa37011226"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token keyword">name</span><span class="token punctuation">,</span> birth<span class="token punctuation">,</span> <span class="token function">MONTH</span><span class="token punctuation">(</span>birth<span class="token punctuation">)</span> <span class="token keyword">FROM</span> pet<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> name     <span class="token punctuation">|</span> birth      <span class="token punctuation">|</span> MONTH(birth) <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> Fluffy   <span class="token punctuation">|</span> 1993<span class="token punctuation">-</span>02<span class="token punctuation">-</span>04 <span class="token punctuation">|</span>            2 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Claws    <span class="token punctuation">|</span> 1994<span class="token punctuation">-</span>03<span class="token punctuation">-</span>17 <span class="token punctuation">|</span>            3 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Buffy    <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>05<span class="token punctuation">-</span>13 <span class="token punctuation">|</span>            5 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Fang     <span class="token punctuation">|</span> 1990<span class="token punctuation">-</span>08<span class="token punctuation">-</span>27 <span class="token punctuation">|</span>            8 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Bowser   <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>08<span class="token punctuation">-</span>31 <span class="token punctuation">|</span>            8 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Chirpy   <span class="token punctuation">|</span> 1998<span class="token punctuation">-</span>09<span class="token punctuation">-</span>11 <span class="token punctuation">|</span>            9 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Whistler <span class="token punctuation">|</span> 1997<span class="token punctuation">-</span>12<span class="token punctuation">-</span>09 <span class="token punctuation">|</span>           12 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Slim     <span class="token punctuation">|</span> 1996<span class="token punctuation">-</span>04<span class="token punctuation">-</span>29 <span class="token punctuation">|</span>            4 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Puffball <span class="token punctuation">|</span> 1999<span class="token punctuation">-</span>03<span class="token punctuation">-</span>30 <span class="token punctuation">|</span>            3 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
  </div>
  <p>
   Finding animals with birthdays in the upcoming month is also
          simple. Suppose that the current month is April. Then the
          month value is
   <code class="literal">
    4
   </code>
   and you can look for
          animals born in May (month
   <code class="literal">
    5
   </code>
   ) like this:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa97150340"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token keyword">name</span><span class="token punctuation">,</span> birth <span class="token keyword">FROM</span> pet <span class="token keyword">WHERE</span> <span class="token function">MONTH</span><span class="token punctuation">(</span>birth<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> name  <span class="token punctuation">|</span> birth      <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> Buffy <span class="token punctuation">|</span> 1989<span class="token punctuation">-</span>05<span class="token punctuation">-</span>13 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
  </div>
  <p>
   There is a small complication if the current month is
          December. You cannot merely add one to the month number
          (
   <code class="literal">
    12
   </code>
   ) and look for animals born in month
   <code class="literal">
    13
   </code>
   , because there is no such month.
          Instead, you look for animals born in January (month
   <code class="literal">
    1
   </code>
   ).
  </p>
  <p>
   You can write the query so that it works no matter what the
          current month is, so that you do not have to use the number
          for a particular month.
   <a class="link" href="date-and-time-functions.html#function_date-add">
    <code class="literal">
     DATE_ADD()
    </code>
   </a>
   enables you to add a
          time interval to a given date. If you add a month to the value
          of
   <a class="link" href="date-and-time-functions.html#function_curdate">
    <code class="literal">
     CURDATE()
    </code>
   </a>
   , then extract the
          month part with
   <a class="link" href="date-and-time-functions.html#function_month">
    <code class="literal">
     MONTH()
    </code>
   </a>
   , the
          result produces the month in which to look for birthdays:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa49557792"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token keyword">name</span><span class="token punctuation">,</span> birth <span class="token keyword">FROM</span> pet
       <span class="token keyword">WHERE</span> <span class="token function">MONTH</span><span class="token punctuation">(</span>birth<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token function">MONTH</span><span class="token punctuation">(</span><span class="token function">DATE_ADD</span><span class="token punctuation">(</span><span class="token function">CURDATE</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token keyword">INTERVAL</span> <span class="token number">1</span> <span class="token keyword">MONTH</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   A different way to accomplish the same task is to add
   <code class="literal">
    1
   </code>
   to get the next month after the current
          one after using the modulo function (
   <code class="literal">
    MOD
   </code>
   )
          to wrap the month value to
   <code class="literal">
    0
   </code>
   if it is
          currently
   <code class="literal">
    12
   </code>
   :
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa15928194"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token keyword">name</span><span class="token punctuation">,</span> birth <span class="token keyword">FROM</span> pet
       <span class="token keyword">WHERE</span> <span class="token function">MONTH</span><span class="token punctuation">(</span>birth<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token function">MOD</span><span class="token punctuation">(</span><span class="token function">MONTH</span><span class="token punctuation">(</span><span class="token function">CURDATE</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   <a class="link" href="date-and-time-functions.html#function_month">
    <code class="literal">
     MONTH()
    </code>
   </a>
   returns a number
          between
   <code class="literal">
    1
   </code>
   and
   <code class="literal">
    12
   </code>
   . And
   <a class="link" href="mathematical-functions.html#function_mod">
    <code class="literal">
     MOD(something,12)
    </code>
   </a>
   returns a
          number between
   <code class="literal">
    0
   </code>
   and
   <code class="literal">
    11
   </code>
   .
          So the addition has to be after the
   <a class="link" href="mathematical-functions.html#function_mod">
    <code class="literal">
     MOD()
    </code>
   </a>
   , otherwise we would go
          from November (
   <code class="literal">
    11
   </code>
   ) to January
          (
   <code class="literal">
    1
   </code>
   ).
  </p>
  <p>
   If a calculation uses invalid dates, the calculation fails and
          produces warnings:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa98994132"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token string">'2018-10-31'</span> <span class="token operator">+</span> <span class="token keyword">INTERVAL</span> <span class="token number">1</span> <span class="token keyword">DAY</span><span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> '2018<span class="token punctuation">-</span>10<span class="token punctuation">-</span>31' <span class="token punctuation">+</span> INTERVAL 1 DAY <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> 2018<span class="token punctuation">-</span>11<span class="token punctuation">-</span>01                    <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token string">'2018-10-32'</span> <span class="token operator">+</span> <span class="token keyword">INTERVAL</span> <span class="token number">1</span> <span class="token keyword">DAY</span><span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> '2018<span class="token punctuation">-</span>10<span class="token punctuation">-</span>32' <span class="token punctuation">+</span> INTERVAL 1 DAY <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> NULL                          <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SHOW</span> <span class="token keyword">WARNINGS</span><span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> Level   <span class="token punctuation">|</span> Code <span class="token punctuation">|</span> Message                                <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> Warning <span class="token punctuation">|</span> 1292 <span class="token punctuation">|</span> Incorrect datetime value: '2018<span class="token punctuation">-</span>10<span class="token punctuation">-</span>32' <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
  </div>
 </div>
 <br/>
</div>
