17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 10: Views<br />

AW00000676 43659 2007-09-30 00:00:00.000<br />

AW00000676 43659 2007-09-30 00:00:00.000<br />

AW00000676 43659 2007-09-30 00:00:00.000<br />

…<br />

…<br />

AW00000227 43662 2007-09-30 00:00:00.000<br />

AW00000227 43662 2007-09-30 00:00:00.000<br />

(51 row(s) affected)<br />

The DATEADD and CAST Functions<br />

The join, while larger than most of the ones we’ve done this far, is still pretty straightforward. We keep<br />

adding tables, joining a column in each new table to a matching column in the tables that we’ve already<br />

named. As always, note that the columns do not have to have the same name — they just have to have<br />

data that relates to one another.<br />

Since this was a relatively complex join, let’s take a look at what we are doing in the query that supports<br />

this view.<br />

The WHERE clause is where things get interesting:<br />

WHERE CAST(soh.OrderDate AS Date) =<br />

CAST(DATEADD(day,-1,GETDATE()) AS Date)<br />

It’s a single comparison, but we used several functions to come up with our result.<br />

It would be very tempting to just compare the OrderDate in the SalesOrderHeader table to GET-<br />

DATE() (today’s date) minus one day — the subtraction operation is what the DATEADD function is<br />

all about. DATEADD can add (you subtract by using negative numbers) any amount of time you want to<br />

deal with. You just tell it what date you want to operate on, and what unit of time you want to add to<br />

it (days, weeks, years, minutes, and so on). On the surface, you should just be able to grab today’s date<br />

with GETDATE() and then use DATEADD to subtract one day. The problem is that GETDATE() returns<br />

the datetime data type and therefore includes the current time of day, so we would get back only rows<br />

from the previous day that happened at the same time of day down to 3.3333 milliseconds (the level of<br />

precision for a datetime field) — not a likely match. So we took things one more step and used the<br />

CAST function to cast the dates on both sides of the equation to a data type that does not support timeof-day-less<br />

before comparison. Therefore, the view will show any sale that happened any time on the<br />

previous date.<br />

Note that this code would not have worked on <strong>SQL</strong> <strong>Server</strong> 2005 and earlier installations, as the Date<br />

data type is new with <strong>SQL</strong> <strong>Server</strong> <strong>2008</strong>. In older versions of <strong>SQL</strong> <strong>Server</strong>, we would use the CONVERT<br />

function to change the date to a varchar formatted without the time, and then perform the comparison.<br />

Using a View to Change Data — Before INSTEAD OF Triggers<br />

308<br />

As we’ve said before, a view works mostly like a table does from an in-use perspective (obviously, creating<br />

them works quite a bit differently). Now we’re going to come across some differences, however.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!