20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

The results of this query are as follows:<br />

MemberId ( MemberId + 2 ) * 3<br />

1 9<br />

4 18<br />

5 21<br />

6 24<br />

7 27<br />

8 30<br />

9 33<br />

As you can see, this time the operations inside the brackets are evaluated first, so for the first row in the<br />

preceding table, the equation reads as follows:<br />

(MemberId + 2) = (1 + 2) = 3<br />

Then the results of the operations inside the brackets are multiplied by 3, as evidenced in the following<br />

equation:<br />

(1 + 2) * 3 = (3) * 3 = 9<br />

So far you’ve used only whole numbers, and nothing with a decimal point. Fractional numbers present<br />

the potential problem of rounding errors. These occur when the number of digits after the decimal point<br />

exceeds either the limitations of a field’s storage type or simply the limitations of the database itself.<br />

Before dealing with fractional numbers, however, you need to have a basic understanding of <strong>SQL</strong>’s four<br />

basic math functions.<br />

Common Math Functions<br />

The <strong>SQL</strong> standard (and almost every <strong>SQL</strong> implementation) contains four basic math functions. However,<br />

a number of other math functions exist, which, while not in the ANSI <strong>SQL</strong> standard, are found in<br />

enough database implementations that it’s worth listing them here, although you have to find out for<br />

yourself which functions your database system supports. While the basic math functions are not earthshattering<br />

in their extent, you should remember that <strong>SQL</strong> and databases are suppliers of raw data and<br />

are not intended to do significant data processing.<br />

The ABS() Function<br />

The ABS() function returns the absolute value of a number — a number without any positive or negative<br />

sign. Basically, it makes negative numbers positive but leaves positive numbers as they are. The function’s<br />

basic format is shown below, where x is the expression to be converted to its absolute value:<br />

ABS(x)<br />

Manipulating Data<br />

159

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

Saved successfully!

Ooh no, something went wrong!