<?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>Luke Hayler - SQL Server developer &#187; dates</title>
	<atom:link href="http://www.lukehayler.com/tag/dates/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lukehayler.com</link>
	<description>A blog about Integration services, Reporting services, and the things that interest me</description>
	<lastBuildDate>Fri, 06 Aug 2010 10:19:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The DatePart() Function in SSRS</title>
		<link>http://www.lukehayler.com/2009/11/the-datepart-function-in-ssrs/</link>
		<comments>http://www.lukehayler.com/2009/11/the-datepart-function-in-ssrs/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 08:00:00 +0000</pubDate>
		<dc:creator>Luke Hayler</dc:creator>
				<category><![CDATA[Reporting Services]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[dateinterval]]></category>
		<category><![CDATA[datepart]]></category>
		<category><![CDATA[dates]]></category>
		<category><![CDATA[expressions]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[SSRS]]></category>

		<guid isPermaLink="false">http://www.lukehayler.com/2009/11/the-datepart-function-in-ssrs/</guid>
		<description><![CDATA[Using the DatePart() Function in SSRS can be tricky, even at the best of times. I have pulled my hair out too often trying to remember what syntax should be used, so have resorted to writing down all my findings. This is so that you don’t have to go through the same agony &#38; frustration [...]]]></description>
			<content:encoded><![CDATA[<p>Using the DatePart() Function in SSRS can be tricky, even at the best of times. I have pulled my hair out too often trying to remember what syntax should be used, so have resorted to writing down all my findings. This is so that you don’t have to go through the same agony &amp; frustration as I did, as well as for my own benefit (my memory is shot…)</p>
<p>What it does: Returns an Integer containing the specified part of a given date</p>
<blockquote><p><strong>Syntax</strong>: DatePart(interval, date[,firstdayofweek[,firstweekofyear]])</p>
</blockquote>
<p>There are two ways to use this function:</p>
<ul>
<li>Using the DateInterval collection </li>
<li>Using a string expression </li>
</ul>
<h5>&#160;</h5>
<h5>Using the DateInterval Collection</h5>
<p>Let’s start with an example:</p>
<blockquote><p>To return the current month: =DatePart(DateInterval.Month, Today())</p>
</blockquote>
<p>Now, in the expression builder (in BIDS), it will look like there’s something the matter with what we have just written:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:8747F07C-CDE8-481f-B0DF-C6CFD074BF67:12d6157d-8b28-4296-909e-c5d87f979888" class="wlWriterEditableSmartContent"><a href="http://www.lukehayler.com/wp-content/uploads/2009/11/SSRS_DatePartFunction_18x6.png" title="Only 'Clippy' is missing...there is nothing wrong with this expression" rel="thumbnail"><img border="0" src="http://www.lukehayler.com/wp-content/uploads/2009/11/SSRS_DatePartFunction_1.png" width="425" height="112" /></a></div>
<p>You can ignore this, the expression <u>will</u> work. An important point to note when using the DateInterval collection, is that you <u>must</u> use a date that is of type Datetime. If you use a string representation of a date (“2009/11/13”), then you’ll need to convert this to Datetime using <em>CDate(“2009/11/13”) </em>before the expression will work. Thus: </p>
<blockquote><p>This will work:&#160; =DatePart(DateInterval.Month, CDate(“2009/11/13”))</p>
<p>This won’t:&#160; = DatePart(DateInterval.Month, “2009/11/13”)</p>
</blockquote>
</p>
<p>When you use the DatePart function to identify the day of the week you can specify what the <em>First Day Of The Week</em> is (defaults to Sunday if this is not specified). You can do this by accessing the <strong>FirstDayOfWeek</strong> collection. Example &#8211; To Return the day of the week, specifying Monday as the first day of the week:</p>
<blockquote><p>DatePart(DateInterval.Weekday, CDate(“2009/11/13”), FirstDayOfWeek.Monday) <strong>returns 5 (Friday)</strong></p>
</blockquote>
<h5>All the possibilities for using the DatePart() function with the DateInterval Collection:</h5>
<p>The following table shows all the possibilities for the DatePart Function using the DateInterval collection. All expressions use a Parameter called MyDate (type = Datetime), which has a value of “2009/11/13 12:34:23”</p>
<table border="1" cellspacing="0" cellpadding="2" width="585">
<tbody>
<tr>
<td valign="top" width="535"><strong>Expression</strong></td>
<td valign="top" width="48"><strong>Result</strong></td>
</tr>
<tr>
<td valign="top" width="535">DatePart(DateInterval.Second, Parameters!MyDate.Value)</td>
<td valign="top" width="48">23</td>
</tr>
<tr>
<td valign="top" width="535">DatePart(DateInterval.Minute, Parameters!MyDate.Value)</td>
<td valign="top" width="48">34</td>
</tr>
<tr>
<td valign="top" width="535">DatePart(DateInterval.Hour, Parameters!MyDate.Value)</td>
<td valign="top" width="48">12</td>
</tr>
<tr>
<td valign="top" width="535">DatePart(DateInterval.Weekday, Parameters!MyDate.Value, FirstDayOfWeek.Monday)</td>
<td valign="top" width="48">5</td>
</tr>
<tr>
<td valign="top" width="535">DatePart(DateInterval.Day, Parameters!MyDate.Value)</td>
<td valign="top" width="48">13</td>
</tr>
<tr>
<td valign="top" width="535">DatePart(DateInterval.DayOfYear, Parameters!MyDate.Value)</td>
<td valign="top" width="48">317</td>
</tr>
<tr>
<td valign="top" width="535">DatePart(DateInterval.WeekOfYear, Parameters!MyDate.Value)</td>
<td valign="top" width="48">46</td>
</tr>
<tr>
<td valign="top" width="535">DatePart(DateInterval.Month, Parameters!MyDate.Value)</td>
<td valign="top" width="48">11</td>
</tr>
<tr>
<td valign="top" width="535">DatePart(DateInterval.Quarter, Parameters!MyDate.Value)</td>
<td valign="top" width="48">4</td>
</tr>
<tr>
<td valign="top" width="535">DatePart(DateInterval.Year, Parameters!MyDate.Value)</td>
<td valign="top" width="48">2009</td>
</tr>
</tbody>
</table>
<p>&#160;</p>
<h5>Using a String Expression</h5>
<p>Again, let’s start with an example:</p>
<blockquote><p>To return the current month:&#160; =DatePart(“m”, Today())</p>
</blockquote>
<p>Using a String expression for your <em>interval </em>enables you to use a date that is of datatype String:</p>
<blockquote><p>=DatePart(“m”, “2009/11/13”)</p>
</blockquote>
<h5>All the possibilities for using the DatePart() function with a String Expression:</h5>
<p>As noted previously, using a string expression for the interval allows you to use a date value that is of the String datatype. You can of course use a Datetime value if you wish to.</p>
<p>This table shows all the possibilities for the DatePart function using a String Expression for the <em>interval.</em> All expressions use a Parameter called MyDate (type = String), which has a value of “2009/11/13 12:34:23”</p>
<table border="1" cellspacing="0" cellpadding="2" width="586">
<tbody>
<tr>
<td valign="top" width="101"><strong>Date Part</strong></td>
<td valign="top" width="430"><strong>Expression</strong></td>
<td valign="top" width="53"><strong>Result</strong></td>
</tr>
<tr>
<td valign="top" width="101">Second</td>
<td valign="top" width="430">DatePart(“s”, Parameters!MyDate.Value)</td>
<td valign="top" width="53">23</td>
</tr>
<tr>
<td valign="top" width="101">Minute</td>
<td valign="top" width="430">DatePart(“n”, Parameters!MyDate.Value)</td>
<td valign="top" width="53">34</td>
</tr>
<tr>
<td valign="top" width="101">Hours</td>
<td valign="top" width="430">DatePart(“h”, Parameters!MyDate.Value)</td>
<td valign="top" width="53">12</td>
</tr>
<tr>
<td valign="top" width="101">Day of Week</td>
<td valign="top" width="430">DatePart(“w”, Parameters!MyDate.Value, FirstDayOfWeek.Monday)</td>
<td valign="top" width="53">5</td>
</tr>
<tr>
<td valign="top" width="101">Day of Month</td>
<td valign="top" width="430">DatePart(“d”, Parameters!MyDate.Value)</td>
<td valign="top" width="53">13</td>
</tr>
<tr>
<td valign="top" width="101">Day of Year</td>
<td valign="top" width="430">DatePart(“y”, Parameters!MyDate.Value)</td>
<td valign="top" width="53">317</td>
</tr>
<tr>
<td valign="top" width="101">Week of Year</td>
<td valign="top" width="430">DatePart(“ww”, Parameters!MyDate.Value)</td>
<td valign="top" width="53">46</td>
</tr>
<tr>
<td valign="top" width="101">Month</td>
<td valign="top" width="430">DatePart(“m”, Parameters!MyDate.Value)</td>
<td valign="top" width="53">11</td>
</tr>
<tr>
<td valign="top" width="101">Quarter</td>
<td valign="top" width="430">DatePart(“q”, Parameters!MyDate.Value)</td>
<td valign="top" width="53">4</td>
</tr>
<tr>
<td valign="top" width="101">Year</td>
<td valign="top" width="430">DatePart(“yyyy”, Parameters!MyDate.Value)</td>
<td valign="top" width="53">2009</td>
</tr>
</tbody>
</table>
<p>&#160;</p>
<h5>Is there a preferred method?</h5>
<p>Ultimately, no. At the end of the day, all the results are the same. What is my preference? I use the DateInterval collection, mainly becuase I like that fact that you can’t get confused about which date part you are using (w/ww, y/yyyy, n….). It’s very clear to anyone who is reading the code that (for example) DateInterval.WeekOfYear refers to the week of the year and nothing else. It also forces you to use a Datetime Value, which safeguards against invalid dates (sort of).</p>
<p>I hope this clears things up a little!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lukehayler.com/2009/11/the-datepart-function-in-ssrs/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>The DateAdd() Function in SSRS</title>
		<link>http://www.lukehayler.com/2009/07/the-dateadd-function-in-ssrs/</link>
		<comments>http://www.lukehayler.com/2009/07/the-dateadd-function-in-ssrs/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 08:00:36 +0000</pubDate>
		<dc:creator>Luke Hayler</dc:creator>
				<category><![CDATA[Reporting Services]]></category>
		<category><![CDATA[dates]]></category>
		<category><![CDATA[expressions]]></category>
		<category><![CDATA[syntax]]></category>

		<guid isPermaLink="false">http://www.lukehayler.com/?p=9</guid>
		<description><![CDATA[Getting acquainted
The DateAdd() function. According to BOL this &#8220;Returns a date to which a specified time interval has been added&#8221;. The structure of the function is as follows:
DateAdd(interval, number, date)
It&#8217;s a Date!
Like most first dates, getting to grips with this function and understanding its &#8216;needs&#8217; is a tricky process. Initially, you may have attempted to [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Getting acquainted</strong></p>
<p>The DateAdd() function. According to BOL this &#8220;Returns a date to which a specified time interval has been added&#8221;. The structure of the function is as follows:</p>
<p><em>DateAdd(interval, number, date)</em></p>
<p><strong>It&#8217;s a Date!</strong></p>
<p>Like most first dates, getting to grips with this function and understanding its &#8216;needs&#8217; is a tricky process. Initially, you may have attempted to use the tried-and-trusted methods (aka &#8217;syntax&#8217;), <em>DateAdd(mm, 1, Now()). </em>No. Ok, let&#8217;s try <em>DateAdd(&#8216;mm&#8217;, 1, now())</em>. Nope. What about <em>DateAdd(month, 1, Now()). </em>Good luck with that&#8230;Yes. It&#8217;s a little mystifying.</p>
<p><strong>Score!</strong></p>
<p>There are a couple of ways to achieve the desired result with this function. 1.) use the <em>DateInterval</em> collection, or 2.) use the date part abbreviations. My recommendation? Use the DateInterval collection. Using the abbreviations, there is a chance that something will be calculated differently to what you&#8217;d expect (I explain a little further on). The <em>DateInterval</em> collection can be found in the Object Browser and looks like this:</p>
<div id="attachment_45" class="wp-caption aligncenter" style="width: 297px"><img class="size-full wp-image-45" title="The DateInterval Collection" src="http://www.lukehayler.com/wp-content/uploads/2009/07/DateAdd_DateIntervals.jpg" alt="A list of all the Intervals in the DateInterval collection" width="287" height="237" /><p class="wp-caption-text">Fig 1. A list of all the Intervals in the DateInterval collection</p></div>
<p>&#8220;So how do you use this in the function?&#8221; I hear you ask.</p>
<p><em>=DateAdd(<strong>DateInterval.Month</strong>, 1, Today())</em> which gives you a date 1 month from today.</p>
<p>This will work for all the intervals listed in Fig 1. above.</p>
<p>If you don&#8217;t want to use this method and instead want to use the abbreviations then there are a few things to be weary of. For instance, using <em>DateAdd(&#8220;w&#8221;, 1, Today())</em> does not yield a date 1 <strong>week</strong> from today, but rather 1 <strong>day</strong> from today. This happens with a couple of other abbreviations as well. The reason is that &#8220;w&#8221; represents &#8220;weekday&#8221; not &#8220;week&#8221;. Here is an shortlist of the abbreviations that do work:</p>
<ul>
<li>Days ["dd"] &#8211; DateAdd(&#8220;dd&#8221;, 1, today())</li>
<li>Weeks ["ww"] &#8211; DateAdd(&#8220;ww&#8221;, 1, today())</li>
<li>Months ["mm"] &#8211; DateAdd(&#8220;mm&#8221;, 1, today())</li>
<li>Years ["yyyy"] &#8211; DateAdd(&#8220;yyyy&#8221;, 1, today())</li>
</ul>
<p>For a full list of abbreviations and further explanation, msdn delivers <a href="http://msdn.microsoft.com/en-us/library/cb7z8yf9(VS.85).aspx">here</a>.</p>
<p>Hopefully this clears up a little of the confusion surrounding the DateAdd() function and the intervals that need to be specified. Of course, if there are other methods that can be used or I&#8217;m all ears.</p>
<p>I like keep an open mind. When working with windows, one must.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lukehayler.com/2009/07/the-dateadd-function-in-ssrs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
