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.

3 8 More Than One Apart<br />

2 2 Ends With Less Than Three<br />

2 8 Ends With Less Than Three<br />

1 7 Ends With Less Than Three<br />

1 0 Ends With Less Than Three<br />

1 1 Ends With Less Than Three<br />

0 2 Ends With Less Than Three<br />

0 4 Ends With Less Than Three<br />

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

There are a few of things to pay particular attention to in how <strong>SQL</strong> <strong>Server</strong> evaluated things:<br />

❑ Even when two conditions evaluate to TRUE, only the first condition is used. For example, the<br />

second-to-last row meets both the first (the last digit is smaller than 3) and third (the last digit is<br />

within 1 of the ProductID) conditions. For many languages, including Visual Basic, this kind of<br />

statement always works this way. If you’re from the C world (or one of many similar languages),<br />

however, you’ll need to remember this when you are coding; no “break” statement is required —<br />

it always terminates after one condition is met.<br />

❑ You can mix and match what fields you’re using in your condition expressions. In this case, we<br />

used SalesOrderID, ProductID, and both together.<br />

❑ You can perform pretty much any expression as long as, in the end, it evaluates to a Boolean result.<br />

Let’s try this out with a slightly more complex example. In this example, we’re not going to do the mixand-match<br />

thing — instead, we’ll stick with just the one column we’re looking at (we could change<br />

columns being tested — but, most of the time, we won’t need to). Instead, we’re going to deal with a<br />

more real-life scenario that I helped solve for a rather large e-commerce site.<br />

The scenario is this: Marketing people really like nice clean prices. They hate it when you apply a 10 percent<br />

markup over cost, and start putting out prices like $10.13, or $23.19. Instead, they like slick prices<br />

that end in numbers like 49, 75, 95, or 99. In our scenario, we’re supposed to create a possible new price<br />

list for analysis, and they want it to meet certain criteria.<br />

If the new price ends with less than 50 cents (such as our previous $10.13 example), then marketing<br />

would like the price to be bumped up to the same dollar amount but ending in 49 cents ($10.49 for our<br />

example). Prices ending with 50 cents to 75 cents should be changed to end in 75 cents, and prices ending<br />

with more than 75 cents should be changed to end with 95 cents. Let’s take a look at some examples<br />

of what they want:<br />

If the New Price Would Be Then It Should Become<br />

$10.13 $10.49<br />

$17.57 $17.75<br />

$27.75 $27.75<br />

$79.99 $79.95<br />

Chapter 11: Writing Scripts and Batches<br />

357

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

Saved successfully!

Ooh no, something went wrong!