<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Fernandez de Quilon &#187; MySql</title>
	<atom:link href="http://www.jefferyfernandez.id.au/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jefferyfernandez.id.au</link>
	<description>between flat screens and grey matter</description>
	<lastBuildDate>Thu, 04 Aug 2011 23:01:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3-aortic-dissection</generator>
		<item>
		<title>MySQL Date Age Calculation</title>
		<link>http://www.jefferyfernandez.id.au/2008/07/02/mysql-date-age-calculation/</link>
		<comments>http://www.jefferyfernandez.id.au/2008/07/02/mysql-date-age-calculation/#comments</comments>
		<pubDate>Wed, 02 Jul 2008 09:03:03 +0000</pubDate>
		<dc:creator>Jeffery</dc:creator>
				<category><![CDATA[MySql]]></category>
		<category><![CDATA[stored routines]]></category>

		<guid isPermaLink="false">http://www.jefferyfernandez.id.au/?p=76</guid>
		<description><![CDATA[I have always had the need to calculate the age as year and month from a MySQL field of type date. No doubt you can achieve this with a bit of PHP, but when you require to do the conversion &#8230; <a href="http://www.jefferyfernandez.id.au/2008/07/02/mysql-date-age-calculation/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I have always had the need to calculate the age as year and month from a MySQL field of type date. No doubt you can achieve this with a bit of PHP, but when you require to do the conversion right from MySQL, you can only go as far as getting the age in &#8220;years&#8221;.</p>
<p>I have used here a MySQL stored routine to achive this:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;">DELIMITER $$
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">FUNCTION</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> <span style="color: #008000;">`my<span style="color: #008080; font-weight: bold;">_</span>database`</span>.<span style="color: #008000;">`getage`</span> $$
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">DEFINER</span><span style="color: #CC0099;">=</span><span style="color: #008000;">`root`</span>@<span style="color: #008000;">`localhost`</span> <span style="color: #990099; font-weight: bold;">FUNCTION</span> <span style="color: #008000;">`getage`</span><span style="color: #FF00FF;">&#40;</span>pdate <span style="color: #999900; font-weight: bold;">DATE</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">RETURNS</span> <span style="color: #000099;">char</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">30</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">DETERMINISTIC</span>
<span style="color: #990099; font-weight: bold;">BEGIN</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> years <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> months <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> days <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">DECLARE</span> current_year <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> current_month <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> current_day <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">DECLARE</span> year_diff <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> month_diff <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> day_diff <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">DECLARE</span> yearstring <span style="color: #000099;">CHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">6</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> monthstring <span style="color: #000099;">CHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">7</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">YEAR</span><span style="color: #FF00FF;">&#40;</span>pdate<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">INTO</span> years<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">MONTH</span><span style="color: #FF00FF;">&#40;</span>pdate<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">INTO</span> months<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">DAY</span><span style="color: #FF00FF;">&#40;</span>pdate<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">INTO</span> days<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">YEAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">CURRENT_DATE</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">INTO</span> current_year<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">MONTH</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">CURRENT_DATE</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">INTO</span> current_month<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">DAY</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">CURRENT_DATE</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">INTO</span> current_day<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #FF00FF;">&#40;</span>current_year <span style="color: #CC0099;">-</span> years<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">INTO</span> year_diff<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #FF00FF;">&#40;</span>current_month <span style="color: #CC0099;">-</span> months<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">INTO</span> month_diff<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #FF00FF;">&#40;</span>current_day <span style="color: #CC0099;">-</span> days<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">INTO</span> day_diff<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #009900;">if</span> <span style="color: #FF00FF;">&#40;</span>current_month <span style="color: #CC0099;">&amp;</span>lt<span style="color: #000033;">;</span> months<span style="color: #FF00FF;">&#41;</span> <span style="color: #009900;">THEN</span>
	<span style="color: #990099; font-weight: bold;">SET</span> month_diff <span style="color: #CC0099;">=</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#40;</span>months <span style="color: #CC0099;">-</span> <span style="color: #008080;">12</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">*</span> <span style="color: #CC0099;">-</span><span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">+</span> current_month<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	<span style="color: #990099; font-weight: bold;">SET</span> year_diff <span style="color: #CC0099;">=</span> <span style="color: #FF00FF;">&#40;</span>year_diff <span style="color: #CC0099;">-</span> <span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #009900;">if</span> <span style="color: #FF00FF;">&#40;</span> month_diff <span style="color: #CC0099;">=</span> <span style="color: #008080;">1</span> <span style="color: #FF00FF;">&#41;</span> <span style="color: #009900;">THEN</span>
	<span style="color: #990099; font-weight: bold;">SET</span> monthstring <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;month&quot;</span><span style="color: #000033;">;</span>
<span style="color: #009900;">ELSE</span>
	<span style="color: #990099; font-weight: bold;">SET</span> monthstring <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;months&quot;</span><span style="color: #000033;">;</span>
<span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #009900;">if</span> <span style="color: #FF00FF;">&#40;</span> year_diff <span style="color: #CC0099;">=</span> <span style="color: #008080;">1</span> <span style="color: #FF00FF;">&#41;</span> <span style="color: #009900;">THEN</span>
	<span style="color: #990099; font-weight: bold;">SET</span> yearstring <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;year&quot;</span><span style="color: #000033;">;</span>
<span style="color: #009900;">ELSE</span>
	<span style="color: #990099; font-weight: bold;">SET</span> yearstring <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;years&quot;</span><span style="color: #000033;">;</span>
<span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #009900;">if</span> <span style="color: #FF00FF;">&#40;</span> year_diff <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span> <span style="color: #FF00FF;">&#41;</span> <span style="color: #009900;">THEN</span>
	RETURN <span style="color: #000099;">CONCAT_WS</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">' '</span><span style="color: #000033;">,</span> month_diff<span style="color: #000033;">,</span> monthstring<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #009900;">ELSE</span>
	<span style="color: #009900;">if</span> <span style="color: #FF00FF;">&#40;</span>month_diff <span style="color: #CC0099;">&amp;</span>gt<span style="color: #000033;">;</span> <span style="color: #008080;">0</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #009900;">THEN</span>
		RETURN <span style="color: #000099;">CONCAT_WS</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">' '</span><span style="color: #000033;">,</span>year_diff<span style="color: #000033;">,</span> yearstring<span style="color: #000033;">,</span> month_diff<span style="color: #000033;">,</span> monthstring<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">ELSE</span>
		RETURN <span style="color: #000099;">CONCAT_WS</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">' '</span><span style="color: #000033;">,</span>year_diff<span style="color: #000033;">,</span> yearstring<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span>
<span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #009900;">END</span> $$
&nbsp;
DELIMITER <span style="color: #000033;">;</span></pre></div></div>

<p>You can then make use of the stored routine to convert Dates into ages by doing so:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> getage<span style="color: #FF00FF;">&#40;</span>BirthDate<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> UserAge <span style="color: #990099; font-weight: bold;">FROM</span> my_database.users</pre></div></div>

<ul>
<li><a title="http://dev.mysql.com/doc/refman/5.0/en/routine-restrictions.html" href="http://dev.mysql.com/doc/refman/5.0/en/routine-restrictions.html" target="_blank">http://dev.mysql.com/doc/refman/5.0/en/routine-restrictions.html</a></li>
<li><a title="http://dev.mysql.com/doc/refman/5.0/en/stored-procedures.html" href="http://dev.mysql.com/doc/refman/5.0/en/stored-procedures.html" target="_blank">http://dev.mysql.com/doc/refman/5.0/en/stored-procedures.html</a></li>
</ul>
<div style="float:left;margin:0px 0px 0px 0px;"></div>]]></content:encoded>
			<wfw:commentRss>http://www.jefferyfernandez.id.au/2008/07/02/mysql-date-age-calculation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

