20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

ased on the digits after the decimal point. If the digit after the decimal point is 4 or less, then it’s simply<br />

removed. If the digit after the decimal point is 5 or more, then the number is rounded up to the next<br />

highest whole number and the digit is removed. For example, 3.55 rounded to a whole number using<br />

the ROUND() function would be 4, whereas 3.42 rounded to a whole number would be 3. It’s important<br />

to remember that with negative numbers, lower means a higher number (-5 is lower in value than -4).<br />

So, -4.6 rounded to the next highest integer is -5. Unlike CEILING() and FLOOR(), the ROUND() function<br />

is supported by MS Access.<br />

Many implementations of the ROUND() function in <strong>SQL</strong> work along these lines: If the number to be truncated,<br />

or cut off, is 4 or less, then it’s removed and the remaining number isn’t changed; if the number is 5<br />

or more, then the remaining number is rounded up. This method of rounding is called the scientific<br />

method. An alternative but less common method is the commercial method of rounding. In the commercial<br />

method, if the digit to be removed is 4 or less, then it is rounded down. If the digit is 6 or more, then it is<br />

rounded up. However, if the digit is 5, then it’s rounded up half the time and rounded down the other half<br />

to prevent rounding errors. All the databases in this book use the scientific method of rounding.<br />

One more difference exists between the ROUND() function and the CEILING() and FLOOR() functions.<br />

The ROUND() function allows you to specify how many digits are permitted after the decimal point,<br />

whereas the FLOOR() and CEILING() functions remove all digits. The ROUND() function, therefore,<br />

requires two values to be passed to it: first, the number to be rounded, and second, the number of digits<br />

allowed after the decimal point. The ROUND() function’s basic syntax is as follows:<br />

ROUND(number_to_be_rounded, number_of_decimal_places)<br />

If you want to limit the results from the DVDPrice field to one decimal place, you would write the following<br />

<strong>SQL</strong>:<br />

SELECT DVDPrice, ROUND(DVDPrice,1)<br />

FROM Films<br />

ORDER BY DVDPrice;<br />

Executing the preceding query produces the following results:<br />

DVDPrice ROUND(DVDPrice,1)<br />

NULL NULL<br />

NULL NULL<br />

NULL NULL<br />

NULL NULL<br />

NULL NULL<br />

2.99 3.00<br />

8.95 9.00<br />

8.95 9.00<br />

9.99 10.00<br />

Manipulating Data<br />

Table continued on following page<br />

167

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

Saved successfully!

Ooh no, something went wrong!