Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use DATEDIFF</p> <pre><code>SELECT DATEDIFF(month, '2000-02-10','2000-01-10'); </code></pre> <p>yields 1 since 02-10 is one month after 01-10</p> <p><strong>month</strong> can be changed to <strong>day</strong> to get the number of days instead.</p> <p>Reference: <a href="http://www.1keydata.com/sql/sql-datediff.html" rel="nofollow">http://www.1keydata.com/sql/sql-datediff.html</a></p> <p><strong>UPDATE:</strong> In MSQL you could use DATEDIFF because it can be passed month as an argument, the MySQL version only returns days. In MySQL you could instead use <a href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_period-diff" rel="nofollow">PERIOD_DIFF</a>. <strong>Although it doesn't know about the exact number of days in each month.</strong> </p> <blockquote> <p>PERIOD_DIFF(P1,P2)</p> </blockquote> <p>"Returns the number of months between periods P1 and P2. P1 and P2 should be in the format YYMM or YYYYMM. Note that the period arguments P1 and P2 are not date values."</p> <pre><code>mysql&gt; SELECT PERIOD_DIFF(200802,200703); -&gt; 11 </code></pre> <p>If you need to know if exactly how many months differ you need to make a multistep calculation. To find out the number of days in a month you could use the <a href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_last-day" rel="nofollow">LAST_DAY</a> function and from the results extract the day part and use as the basis for further calculations.</p> <pre><code> LAST_DAY('2003-02-05'); -&gt; '2003-02-28' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload