<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SSIS Junkie : expressions, SQL Server Reporting Services</title><link>http://sqlblog.com/blogs/jamie_thomson/archive/tags/expressions/SQL+Server+Reporting+Services/default.aspx</link><description>Tags: expressions, SQL Server Reporting Services</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Fun and games with Reporting Services expressions</title><link>http://sqlblog.com/blogs/jamie_thomson/archive/2009/12/10/fun-and-games-with-reporting-services-expressions.aspx</link><pubDate>Thu, 10 Dec 2009 16:23:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:19741</guid><dc:creator>jamiet</dc:creator><slash:comments>6</slash:comments><comments>http://sqlblog.com/blogs/jamie_thomson/comments/19741.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/jamie_thomson/commentrss.aspx?PostID=19741</wfw:commentRss><wfw:comment>http://sqlblog.com/blogs/jamie_thomson/rsscomments.aspx?PostID=19741</wfw:comment><description>&lt;p&gt;I have today been messing about with Reporting Services' expression language and as such have learnt a few things that I figured might be worth sharing.&lt;/p&gt;  &lt;p&gt;The report that I have been attempting to build has two parameters that define the effective reporting period for the report, effectively they are arguments in a WHERE clause:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/jamie_thomson/20091210params_76BF90FC.jpg"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="20091210params" border="0" alt="20091210params" src="http://sqlblog.com/blogs/jamie_thomson/20091210params_thumb_2E91D520.jpg" width="565" height="36" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The business rule that I had to implement for these parameters was:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;i&gt;The effective reporting period is the most recently completed month&lt;/i&gt;      &lt;br /&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;In other words, I had to determine the first day and last day of the previous month which for me (as I was trying to do this on 8th December 2009) was 1st November 2009 and 30th November 2009. &lt;/p&gt;  &lt;p&gt;How would you have gone about this? Here was my first attempt at determining &amp;quot;From Effective Date&amp;quot; (i.e. 1st November 2009) based on today's date:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span&gt;=CDate(       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; CStr(        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Year(DateAdd(&amp;quot;mm&amp;quot;, -1, Now())) * 10000 +        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Month(DateAdd(&amp;quot;mm&amp;quot;, -1, Now())) * 100 +        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; )        &lt;br /&gt;)&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;You can probably decipher my logic here. Subtract one month from today's date and use the first day of the month of the result. Sound logic but unfortunately it failed with a &lt;a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=519531#details" target="_blank"&gt;totally unhelpful error message &lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;b&gt;&amp;quot;&lt;span&gt;An error occurred during local report processing. &amp;lt;parameter name&amp;gt;&lt;/span&gt;&amp;quot;&lt;/b&gt;      &lt;br /&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;After investigation it seems as though the CDate() function does not like strings in the format YYYYMMDD so here was my next attempt:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;=CDate(     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; CStr(Year(DateAdd(DateInterval.Month,-1,Now()))) + &amp;quot;-&amp;quot; +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; RIGHT(&amp;quot;0&amp;quot; + CStr(Month(DateAdd(DateInterval.Month,-1,Now()))), 2) +       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;quot;-01&amp;quot;      &lt;br /&gt;)&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This one worked fine and I was going to stick with it until &lt;a href="http://twitter.com/summitcloud"&gt;Summitcloud&lt;/a&gt; suggested using the DateSerial(...) function instead like so:&lt;/p&gt;  &lt;p align="center"&gt;=DateSerial(Year(&lt;font color="#0000ff"&gt;DateAdd(DateInterval.Month,-1, Now()))&lt;/font&gt;, &lt;font color="#008000"&gt;Month(DateAdd(DateInterval.Month,-1, Now()))&lt;/font&gt;, &lt;font color="#ff00ff"&gt;1&lt;/font&gt;)&lt;/p&gt;  &lt;p&gt;That's a much nicer method in my opinion, no hacky string manipulation going on here, only three numeric arguments (which I have highlighted in different colours) to define year/month/day thus I changed to use DateSerial(…). Happy with that I turned to working out my &amp;quot;To Effective Date&amp;quot; using the same DateSerial() function. This time my logic was to get the first day of the &lt;i&gt;current&lt;/i&gt; month and then subtract one day from it:&lt;/p&gt;  &lt;p align="center"&gt;=DateAdd(DateInterval.Day, -1, DateSerial(&lt;font color="#0000ff"&gt;Year(Now())&lt;/font&gt;, &lt;font color="#008000"&gt;Month(Now())&lt;/font&gt;, &lt;font color="#ff00ff"&gt;1&lt;/font&gt;))&lt;/p&gt;  &lt;p&gt;Worked a treat!&lt;/p&gt;  &lt;p&gt;So I guess the lesson here is to make sure you know all the tools in your toolbox and what they all do. Don't use pliers (string manipulation) to tighten a nut when you already have a spanner(DateSerial)! (Does that analogy work? Maybe not!! :)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://twitter.com/jamiet" target="_blank"&gt;@Jamiet&lt;/a&gt;&lt;/p&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=19741" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/jamie_thomson/archive/tags/sql+server/default.aspx">sql server</category><category domain="http://sqlblog.com/blogs/jamie_thomson/archive/tags/SQL+Server+Reporting+Services/default.aspx">SQL Server Reporting Services</category><category domain="http://sqlblog.com/blogs/jamie_thomson/archive/tags/expressions/default.aspx">expressions</category></item></channel></rss>