<?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>Vince Plaza&#039;s Blog &#187; Beginner</title>
	<atom:link href="http://www.vinceplaza.com/category/skill-level/introductory/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vinceplaza.com</link>
	<description>Microsoft .NET programming ideas and samples</description>
	<lastBuildDate>Sat, 13 Mar 2010 00:07:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Downloading and Installing Microsoft&#8217;s Report Builder 2</title>
		<link>http://www.vinceplaza.com/2010/03/12/downloading-and-installing-microsofts-report-builder-2/</link>
		<comments>http://www.vinceplaza.com/2010/03/12/downloading-and-installing-microsofts-report-builder-2/#comments</comments>
		<pubDate>Sat, 13 Mar 2010 00:04:48 +0000</pubDate>
		<dc:creator>Vince Plaza</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Report Builder 2]]></category>
		<category><![CDATA[Reporting Services]]></category>

		<guid isPermaLink="false">http://www.vinceplaza.com/?p=235</guid>
		<description><![CDATA[Report Builder 2 is Microsoft&#8217;s latest tool for power-users to create ad hoc data reports. The tool is different than Report Builder 1 in a significant way. Report Builder 1 was specific to Reporting Services 2005 and requred that a data source be pre-built on the target server. Report Builder 2, although designed for publishing [...]]]></description>
			<content:encoded><![CDATA[<p>Report Builder 2 is Microsoft&#8217;s latest tool for power-users to create ad hoc data reports. The tool is different than Report Builder 1 in a significant way. Report Builder 1 was specific to Reporting Services 2005 and requred that a data source be pre-built on the target server. Report Builder 2, although designed for publishing shared reports to Reporting Services 2008, is usable against any ODBC data source that the user has permissions to. It is feasible that reports are built and consumed by the user on their client machine without ever publishing or accessing a data source on a server. </p>
<p>This article shows how to download and install Report Builder 2 on a local machine. For a good article on how to enable the product on a Reporting Services 2008 instance, see the following SQL Server Magazine article (subscription required): </p>
<p><a href="http://www.sqlmag.com/article/reporting-services/report-builder-2-0.aspx">http://www.sqlmag.com/article/reporting-services/report-builder-2-0.aspx</a></p>
<p>You can download the application from Microsoft at the following location:</p>
<p><a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=9f783224-9871-4eea-b1d5-f3140a253db6&#038;displaylang=en"/>http://www.microsoft.com/downloads/details.aspx?FamilyID=9f783224-9871-4eea-b1d5-f3140a253db6&#038;displaylang=en</a></p>
<p>After downloading the file ReportBuilder.msi to your target system, double click it to begin the install process. You will be walked thought the usual license acceptance and user name dialog. However, when you get to the form looking for the default report server pause. If you have an instance of Reporting Services 2008 in your organization where you can save reports to a &#8220;My Reports&#8221; folder, enter the URL here. The URL is usually in the format &#8220;http//someserver/reportserver&#8221;. If you leave this blank, Report Builder will default to saving RDL files to your local documents folder instead. </p>
<p><a href="http://www.vinceplaza.com/wp-content/uploads/2010/03/ReportBuilder2DefaultReportServer.jpg"><img src="http://www.vinceplaza.com/wp-content/uploads/2010/03/ReportBuilder2DefaultReportServer-300x227.jpg" alt="[Picture of default report server dialog]" title="Report Builder 2 Default Report Server" width="300" height="227" class="alignnone size-medium wp-image-239" /></a></p>
<p>After the installation is finished, you can find the application in your start menu under &#8220;Microsoft SQL Server 2008 Report Builder 2.0&#8243;. Launch the application and you will be greeted with the main window that looks like the following. In future posts, I will go into how to connect to databases and create simple reports. </p>
<p><a href="http://www.vinceplaza.com/wp-content/uploads/2010/03/ReportBuilder2MainWindow3.jpg"><img src="http://www.vinceplaza.com/wp-content/uploads/2010/03/ReportBuilder2MainWindow3-300x232.jpg" alt="[The main window of the Report Builder 2 application]" title="Report Builder 2 Main Window" width="300" height="232" class="alignnone size-medium wp-image-243" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.vinceplaza.com/2010/03/12/downloading-and-installing-microsofts-report-builder-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Sort on the last name in full name field using T-SQL string functions</title>
		<link>http://www.vinceplaza.com/2009/10/20/sort-on-the-last-name-in-full-name-field/</link>
		<comments>http://www.vinceplaza.com/2009/10/20/sort-on-the-last-name-in-full-name-field/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 23:11:38 +0000</pubDate>
		<dc:creator>Vince Plaza</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.vinceplaza.com/?p=220</guid>
		<description><![CDATA[A friend asked me if it was possible to sort on just a portion of a text field, but not necessarily the first word. For example, lets say you have a full name field that is formatted  as &#8216;[FirstName] [LastName]&#8216;. There is a simple, if not flawless, way to do this with T-SQL string functions. [...]]]></description>
			<content:encoded><![CDATA[<p>A friend asked me if it was possible to sort on just a portion of a text field, but not necessarily the first word. For example, lets say you have a full name field that is formatted  as &#8216;[FirstName] [LastName]&#8216;. There is a simple, if not flawless, way to do this with T-SQL string functions. First, you need to generate a small list of names to work with. I will use a table variable to do this. It is easy to spin up and is disposed of as soon as the query is done running.</p>
<p><code>declare @MyNames table (FullName nvarchar(20));<br />
insert @MyNames<br />
values ('Betty Ferguson'),('Sam L Jackson'),('Horatio Alger');</code></p>
<p>Note that I used the much simpler SQL Server 2008 syntax to add the names. If you are using SQL Server 2005, you will need to add the names like this:</p>
<p><code>insert @MyNames<br />
values ('Betty Ferguson');<br />
insert @MyNames<br />
values ('Sam L Jackson');<br />
insert @MyNames<br />
values ('Horatio Alger');</code></p>
<p>Since you can&#8217;t control people adding a middle initial, I will find the last name by counting the number of characters starting from the right before I find a space character. The best function to use for this is charindex.</p>
<p><code>select charindex(' ', FullName) as 'LastNameLetterCount' from @MyNames;</code></p>
<p>The problem is that the charindex function counts from the left. In order to count from the right, I need to flop the text over by using the reverse function. This function will take a string of characters such as &#8216;My dog spot&#8217; and return it as &#8216;tops god yM&#8217;. I also subtract 1 so that I only return the length of the last name, not the last name and the space.<br />
<span id="more-220"></span><br />
<code>select charindex(' ', reverse(FullName))-1 as 'LastNameLetterCount' from @MyNames;</code></p>
<p>This returns the following values:</p>
<p>LastNameLetterCount<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
8<br />
7<br />
5</p>
<p>Now I have the length of our last name. With this value, I can use the &#8216;right&#8217; function to isolate the last name value. This function will return the number of characters we indicate starting from the right. For instance right(&#8216;My String&#8217;,3) will return &#8216;ing&#8217;.</p>
<p><code>select right(FullName, charindex(' ', reverse(FullName))-1) as 'LastName' from @MyNames;</code></p>
<p>This will return the values:</p>
<p>LastName<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
Ferguson<br />
Jackson<br />
Alger</p>
<p>However, I want to order by this field, not display it. To do that, I move the string functions to the &#8216;order by&#8217; clause and only display the original field.</p>
<p><code>select   FullName<br />
from     @MyNames<br />
order by right(FullName, charindex(' ', reverse(FullName))-1);</code></p>
<p>This will return the requested field in the desired ascending order:</p>
<p>FullName<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
Horatio Alger<br />
Betty Ferguson<br />
Sam L Jackson</p>
<p>Of course to sort in reverse order, I could add the &#8216;desc&#8217; flag to the order by clause:</p>
<p><code>select   FullName<br />
from     @MyNames<br />
order by right(FullName, charindex(' ', reverse(FullName))-1) desc;</code></p>
<p>FullName<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
Sam L Jackson<br />
Betty Ferguson<br />
Horatio Alger</p>
<p>You may have noticed the holes in this approach, such as the field only containing a first name, a two part last name, etc. However, for some quick querying on a known data set, this technique may get you pretty far.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vinceplaza.com/2009/10/20/sort-on-the-last-name-in-full-name-field/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use a view to encapsulate business logic and reduce code</title>
		<link>http://www.vinceplaza.com/2009/08/28/use-a-view-to-encapsulate-business-logic-and-reduce-code/</link>
		<comments>http://www.vinceplaza.com/2009/08/28/use-a-view-to-encapsulate-business-logic-and-reduce-code/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 03:36:02 +0000</pubDate>
		<dc:creator>Vince Plaza</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.vinceplaza.com/?p=153</guid>
		<description><![CDATA[This post adds to a previous post:Working with additive values in tables with different levels of detail
In the last post, we used a subquery to only show the latest value in a table that has a time component. The table holds an employee&#8217;s pay rate history and includes a &#8220;RateChangeDate&#8221; field. Although our use of [...]]]></description>
			<content:encoded><![CDATA[<p>This post adds to a previous post:<a href="http://www.vinceplaza.com/2009/08/25/working-with-additive-values-in-tables-with-different-levels-of-detail/">Working with additive values in tables with different levels of detail</a></p>
<p>In the last post, we used a subquery to only show the latest value in a table that has a time component. The table holds an employee&#8217;s pay rate history and includes a &#8220;RateChangeDate&#8221; field. Although our use of a subquery makes sense, over time it will become tedious to type the same code every time we want to show an employee&#8217;s current pay rate. In addition, the use of the subquery adds risk to the quality of our application. The business logic represented by the subquery will need to be tested every place the subquery appears, increasing the odds for an error down the road.</p>
<p>Since we know the subquery is a handy block of code, we will take advantage of a common database feature called a &#8220;view&#8221;. In a nutshell, a view is a persistent select query that is given a name and can be referenced just like a table. With a view, business logic can be defined once and shared between future queries and even between programmers.</p>
<p>Taking our last example, lets turn the subquery for the latest pay rate and turn it into a view. Note how we use the &#8220;create&#8221; statement just like when making a table.<br />
<code>create view HumanResources.CurrentEmployeePayrateView<br />
as<br />
select    e.EmployeeId<br />
          ,eph.Rate as Payrate<br />
from      (<br />
               select   EmployeeId<br />
                        ,max(RateChangeDate) as RateChangeDate<br />
               from     HumanResources.EmployeePayHistory<br />
               group by EmployeeId<br />
           ) e<br />
inner join HumanResources.EmployeePayHistory eph on e.EmployeeId = eph.EmployeeId<br />
                                    and e.RateChangeDate = eph.RateChangeDate<br />
</code><br />
<span id="more-153"></span><br />
Now we can replace the subquery in our query from the last post with a reference to our view like this:<br />
<code>select       e.EmployeeId<br />
             ,c.LastName + ', ' + c.FirstName as EmployeeName<br />
             ,e.VacationHours + e.SickLeaveHours - 120 as AccruedPtoHoursInExcess<br />
             ,cast((e.VacationHours + e.SickLeaveHours - 120) * f.Rate as decimal(6,2)) as Liability<br />
from         HumanResources.Employee e<br />
inner join   Person.Contact c on e.ContactId = c.ContactId<br />
inner join   HumanResources.CurrentEmployeePayRateView f on e.EmployeeId = f.EmployeeId                            <br />
where        VacationHours + SickLeaveHours &gt; 120;</code></p>
<p>A couple of notes: The view acts like a table in more ways than one. You can set permissions on it for specific user by using the &#8220;grant select&#8221; statement just like on a table. It is also possible to put an index on a view, but I do not recommend this for beginners as an index on a view can become complicated to manage. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.vinceplaza.com/2009/08/28/use-a-view-to-encapsulate-business-logic-and-reduce-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Working with additive values in tables with different levels of detail</title>
		<link>http://www.vinceplaza.com/2009/08/25/working-with-additive-values-in-tables-with-different-levels-of-detail/</link>
		<comments>http://www.vinceplaza.com/2009/08/25/working-with-additive-values-in-tables-with-different-levels-of-detail/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 17:44:14 +0000</pubDate>
		<dc:creator>Vince Plaza</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.vinceplaza.com/?p=126</guid>
		<description><![CDATA[Downloads: Sample Database
There are times when you need to perform a calculation on values stored in two different tables. The first value is stored in a summary level table, and the second is stored in a detail table. I will use the Adventure Works employee tables to demonstrate.
Let&#8217;s say you need to calculate the accrued [...]]]></description>
			<content:encoded><![CDATA[<p>Downloads: <a href="http://msftdbprodsamples.codeplex.com/">Sample Database</a></p>
<p>There are times when you need to perform a calculation on values stored in two different tables. The first value is stored in a summary level table, and the second is stored in a detail table. I will use the Adventure Works employee tables to demonstrate.</p>
<p>Let&#8217;s say you need to calculate the accrued vacation time for your employees that exceeds the company limit of 120 hours and derive the total liability to the company in terms of dollars. Using the Adventure Works database, that would require three tables: HumanResources.Employee, Person.Contact, and HumanResources.EmployeePayHistory. To start, we know we need to identify the employee so we include the EmployeeId field from the Employee table and the LastName and FirstName fields from the Contact table. Make sure and include the schema when declaring the tables, as this is required for any schema that is not &#8216;dbo&#8217;. This is a good practice anyway. Also, note the use of the table aliases &#8216;e&#8217; and &#8216;c&#8217; to save typing and add clarity.</p>
<p><code>select      e.EmployeeId<br />
            ,c.LastName + ', ' + c.FirstName as EmployeeName<br />
from        HumanResources.Employee e<br />
inner join  Person.Contact c on e.ContactId = c.ContactId</code><br />
<span id="more-126"></span><br />
Now we can easily calculate the accrued hours by adding two fields from the employees table: VacationHours and SickLeaveHours. We are assuming the business definition for the field is &#8216;hours accrued&#8217;, but this would need to be confirmed with the business owner. Since the column does not allow nulls, we do not need to trap for that using the ISNULL or COALESCE function. However, don&#8217;t forget to subtract the acceptable limit of 120 so we only see the delta.</p>
<p><code>select      e.EmployeeId<br />
            ,c.LastName + ', ' + c.FirstName as EmployeeName<br />
            ,e.VacationHours + e.SickLeaveHours - 120 as AccruedPtoHoursInExcess<br />
from        HumanResources.Employee e<br />
inner join  Person.Contact c on e.ContactId = c.ContactId</code></p>
<p>Since this will result in negative numbers for employees that have not accrued at least 120 hours, we might decide to trap that using the CASE statement and only return either a zero or a positive number. For our purposes, though, it makes sense to just show employees with a positive number. We will add a WHERE clause to limit our list. As a good practice, you should limit your results earlier in your development process or risk overburdening your database with an oversize result set. If you do not what your WHERE clause will be, you can still add a TOP declaration to your SELECT statement and limit the results to a manageable number of rows.  It is also a good idea to end your T-SQL statements with a semicolon, so I added one to our query.</p>
<p><code>select      e.EmployeeId<br />
            ,c.LastName + ', ' + c.FirstName as EmployeeName<br />
            ,e.VacationHours + e.SickLeaveHours - 120 as AccruedPtoHoursInExcess<br />
from        HumanResources.Employee e<br />
inner join  Person.Contact c on e.ContactId = c.ContactId<br />
where       e.VacationHours + e.SickLeaveHours &gt; 120;</code></p>
<p>Now for the tricky part. The pay rate for the employees is stored in the EmployeePayHistory table, but the table has a time component. In other words, there is a record added every time the pay rate changes. If we just add the table like follows, then we risk returning more than one record for a single employee. Note the new formula to caculate liability. I use parenthesis to enforce the correct order of operations:</p>
<p><code>select      e.EmployeeId<br />
            ,c.LastName + ', ' + c.FirstName as EmployeeName<br />
            ,VacationHours + SickLeaveHours - 120 as AccruedPtoHoursInExcess<br />
            ,cast((VacationHours + SickLeaveHours - 120) * eph.Rate as decimal(6,2)) as Liability<br />
from        HumanResources.Employee e<br />
inner join  Person.Contact c on e.ContactId = c.ContactId<br />
inner join  HumanResources.EMployeePayHistory eph on e.EmployeeId = eph.EmployeeId<br />
where       VacationHours + SickLeaveHours &gt; 120;</code></p>
<p>In my database, the latest query increases the row count from 97 to 99 because Rob Walters ends up with three records. We could just assume that Rob&#8217;s pay rate always increases, but that is prone to error. Instead, we need to add a subquery to only filter on the latest pay rates for each employee as determined by the RateChangeDate. The following query provides the filter we need:</p>
<p><code>select   EmployeeId<br />
         ,max(RateChangeDate) as RateChangeDate<br />
from     HumanResources.EmployeePayHistory<br />
group by EmployeeId</code></p>
<p>Now, all we need to do is add this into our main query so that it acts like a filter table, and join the original  EmployeePayHistory table to the derived filter table instead:</p>
<p><code>select      e.EmployeeId<br />
            ,c.LastName + ', ' + c.FirstName as EmployeeName<br />
            ,VacationHours + SickLeaveHours - 120 as AccruedPtoHoursInExcess<br />
            ,cast((VacationHours + SickLeaveHours - 120) * eph.Rate as decimal(6,2)) as Liability<br />
from        HumanResources.Employee e<br />
inner join  Person.Contact c on e.ContactId = c.ContactId<br />
inner join  (<br />
                select   EmployeeId<br />
                         ,max(RateChangeDate) as RateChangeDate<br />
                from     HumanResources.EmployeePayHistory<br />
                group by EmployeeId<br />
            ) f on e.EmployeeId = f.EmployeeId<br />
inner join  HumanResources.EmployeePayHistory eph on f.EmployeeId = eph.EmployeeId<br />
                                                    and f.RateChangeDate = eph.RateChangeDate<br />
where       VacationHours + SickLeaveHours &gt; 120;</code></p>
<p>The query now returns our original 97 rows and shows the current liability which is defined as &#8216;the current pay rate times the accrued PTO over 120 hours&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vinceplaza.com/2009/08/25/working-with-additive-values-in-tables-with-different-levels-of-detail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
