<?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/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>JMSoftware &#187; SQL</title>
	<atom:link href="http://www.jmsoftware.co.uk/blog/tag/sql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.jmsoftware.co.uk</link>
	<description>Software Development</description>
	<lastBuildDate>Mon, 26 Sep 2011 13:38:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
		<item>
		<title>Getting the next n birthdays from a table in SQL</title>
		<link>http://www.jmsoftware.co.uk/blog/getting-the-next-n-birthdays-from-a-table-in-sql</link>
		<comments>http://www.jmsoftware.co.uk/blog/getting-the-next-n-birthdays-from-a-table-in-sql#comments</comments>
		<pubDate>Sun, 26 Oct 2008 14:10:35 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[birthdays]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[upcoming]]></category>

		<guid isPermaLink="false">http://www.jmsoftware.co.uk/?p=20</guid>
		<description><![CDATA[If we have a table with a basic structure like this: ---------- person ---------- person_id name dob ... ---------- Let&#8217;s run a query with the results sorted by order of upcoming birthdays: SELECT *, IF(MONTH(`dob`) &#60; MONTH(NOW()) &#124;&#124; (MONTH(`dob`) = &#8230; <a href="http://www.jmsoftware.co.uk/blog/getting-the-next-n-birthdays-from-a-table-in-sql">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If we have a table with a basic structure like this:</p>
<pre><code>----------
person
----------
person_id
name
dob
...
----------</code></pre>
<p>Let&#8217;s run a query with the results sorted by order of upcoming birthdays:</p>
<pre><code>SELECT *,
IF(MONTH(`dob`) &lt; MONTH(NOW()) ||
	(MONTH(`dob`) = MONTH(NOW()) &amp;&amp; DAY(`dob`) &lt; DAY(NOW())),
	MONTH(`dob`) + 12, MONTH(`dob`)) AS `adjusted_month`
FROM `person`
ORDER BY `adjusted_month`, DAY(`dob`), `name`</code></pre>
<p>An alias called adjusted_month is used.</p>
<p>If the month of birth is earlier then the current month, or if it is the same and the day of birth is earlier then the current day, adjusted_month is set to the the month of birth + 12. Otherwise adjusted_month is just set to the month of birth.</p>
<p>The results can then be sorted by adjusted_month and the day to get the desired result.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jmsoftware.co.uk/blog/getting-the-next-n-birthdays-from-a-table-in-sql/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>

