<?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; dateinterval</title>
	<atom:link href="http://lukehayler.com/tag/dateinterval/feed/" rel="self" type="application/rss+xml" />
	<link>http://lukehayler.com</link>
	<description>A blog about Business Intelligence and other personal interests</description>
	<lastBuildDate>Tue, 13 Dec 2011 11:42:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>The DatePart() Function in SSRS</title>
		<link>http://lukehayler.com/2009/11/the-datepart-function-in-ssrs/</link>
		<comments>http://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[Luke Hayler]]></category>
		<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. &#8230; <a href="http://lukehayler.com/2009/11/the-datepart-function-in-ssrs/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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://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://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>Ul<br />
timately, 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://lukehayler.com/2009/11/the-datepart-function-in-ssrs/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>

