<?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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Somando</title>
	<atom:link href="http://somando.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://somando.wordpress.com</link>
	<description>My random thoughts and ideas</description>
	<lastBuildDate>Fri, 09 Jul 2010 07:12:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='somando.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/8e492b2ecded76d02b510f9c304d8968?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Somando</title>
		<link>http://somando.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://somando.wordpress.com/osd.xml" title="Somando" />
	<atom:link rel='hub' href='http://somando.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Oracle SQL &#8211; Finding Monday</title>
		<link>http://somando.wordpress.com/2010/07/08/oracle-sql-finding-monday/</link>
		<comments>http://somando.wordpress.com/2010/07/08/oracle-sql-finding-monday/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 23:58:08 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Oracle SQL]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[iso]]></category>
		<category><![CDATA[monday]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[trunc]]></category>

		<guid isPermaLink="false">https://somando.wordpress.com/?p=90</guid>
		<description><![CDATA[Today I was writing a piece of software that featured time-based input where you enter data for a week period. The interface requirements for this software specified that the week would always start on a Monday, and it should default &#8230; <a href="http://somando.wordpress.com/2010/07/08/oracle-sql-finding-monday/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=somando.wordpress.com&amp;blog=8324539&amp;post=90&amp;subd=somando&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I was writing a piece of software that featured time-based input where you enter data for a week period. The interface requirements for this software specified that the week would always start on a Monday, and it should default to showing the current week for input.</p>
<p>I remember when I first worked with Oracle back in college, when we worked with some crazy date function combinations to produce results for insane date requirements. I assumed I would end up needing some of the date functions we used back then, maybe even needing decodes to get the date of Monday.</p>
<p>I knew that TRUNC accepted dates (I use it all the time when comparing dates) and that it accepted parameters for how to truncate the date (by day, week, month or year). I found that I could use the &#8216;Week&#8217; flag to find the start date of a given week. The first time I ran this, using</p>
<p><code>select trunc(sysdate, 'week') from dual;</code></p>
<p>the database returned exactly what I had hoped for: 05-JUL-10, the date of the Monday just gone. My experience with Oracle development over the past 3 years thought me to be cautious with this however, and sure enough running the same query on another environment brought back Sunday instead of Monday, because the NLS parameters were different.</p>
<p>Because the software I was writing would be deployed to a wide variety of customers, it was not safe to make any assumptions about environmental parameters, so I decided to look again. At first glance of Oracle&#8217;s documentation, I passed by on the &#8216;IW&#8217; flag, which refers to the ISO week code. The docs made it sound as though it was more geared towards finding the day the 1st of January fell on this year, so I kept looking.</p>
<p>After some browsing, however, I found more evidence that using this flag was exactly what I needed. I tried it out, and it worked flawlessly, consistently returning the Monday date value, across multiple environments with different NLS parameter configurations. So if you need to find the Monday for a date in a given week, use the following query:</p>
<p><code>select trunc(sysdate, 'IW') from dual;</code></p>
<p>The bonus of this is that it also makes it simple to find thr value for any other day of the week? Simply add the number of days between Monday and the day you&#8217;re looking for (add 1 for Tuesday, 2 for Wednesday, and so on, up to adding 6 for Sunday) as follows:</p>
<p><code>select trunc(sysdate, 'IW') + 1 from dual;</code></p>
<p><strong> Useful related links:</strong></p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/ISO_8601#Week_dates" target="_blank">ISO 8601 wikipedia entry</a></li>
<li><a href="http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions230.htm#i1002084" target="_blank">Oracle Database SQL Reference: ROUND and TRUNC Date Functions</a></li>
<li><a href="http://www.techonthenet.com/oracle/functions/trunc_date.php" target="_blank">Tech on the Net Oracle PL/SQL Reference: Trunc Function (with Dates)</a></li>
<li><a href="http://psoug.org/reference/date_func.html" target="_blank">PSOUG.org Oracle Reference: Date Functions</a></li>
</ul>
<br />Filed under: <a href='http://somando.wordpress.com/category/oracle-sql/'>Oracle SQL</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/somando.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/somando.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/somando.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/somando.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/somando.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/somando.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/somando.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/somando.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/somando.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/somando.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/somando.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/somando.wordpress.com/90/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/somando.wordpress.com/90/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/somando.wordpress.com/90/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=somando.wordpress.com&amp;blog=8324539&amp;post=90&amp;subd=somando&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://somando.wordpress.com/2010/07/08/oracle-sql-finding-monday/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>51.825150 -8.391384</georss:point>
		<geo:lat>51.825150</geo:lat>
		<geo:long>-8.391384</geo:long>
		<media:content url="http://0.gravatar.com/avatar/ca3b5090da3ec4ee27c837e3626a6dd2?s=96&#38;d=identicon&#38;r=X" medium="image">
			<media:title type="html">Somando</media:title>
		</media:content>
	</item>
	</channel>
</rss>
