Binary Operators
|
The Addition
|
The addition, also called the sum, is an operation used to add one item to
another.
The addition is performed using the + sign.
To get the addition of two values, you type + between them, as in Value1 to
Value2.
After the addition has been performed, you get a new value that you can
make available or display to the user. You can perform the addition on two
numbers. Here is an example:
SELECT 125 + 4088
You can also add more
than two values, like a + b + c.
The order you use to add two or more values doesn't matter. This means
that Value1 + Value2 is the same as Value2 + Value1. In the same way a + b + c
is the same as a + c + b the same as b + a + c and the same as c + b + a.
This means that the addition is associative as illustrated in the
following statement:
SELECT 128 + 42 + 5; SELECT 5 + 42 + 128; GO ----------- 175 (1 row affected) ----------- 175 (1 row affected) 1> |
The Subtraction
|
The subtraction operation, sometimes called the
difference, is used to take out or
subtract one value from another value. It is essentially the opposite of the
addition. The subtraction is performed with the - sign. Here is an example:
|
SELECT 1240 - 608 |
Unlike the addition, the subtraction operation is not
associative. This means that a - b - c is not necessarily equal to c - b -
a. This is illustrated in the following statements:
|
SELECT 128 - 42 - 5; SELECT 5 - 42 - 128; GO |
----------- 81 (1 row affected) ----------- -165 (1 row affected) 1>
Notice that both operations of the addition convey the
same result. In the subtraction section, the numbers follow the same order
but a different operation; and the last two operations render different
results.
|
The Multiplication
|
The multiplication allows adding one value to itself a
certain number of times, set by a second value. As an example, instead of
adding a value to itself in this manner: a + a + a + a, since the variable
a is repeated over and over again, you could simply find out how many
times a is added to itself, then multiply a by that number which, is this
case, is 4. This would mean adding a to itself 4 times, and you would get
the same result.
The multiplication is performed with the * sign. Just like the addition, the multiplication is associative: a * b * c = c * b * a. Here is an example: |
SELECT 128 * 42
This would produce 5376
|
The Division
|
The division operation is similar to cutting an item in pieces or
fractions of a set value.
Therefore, the division is used to get the fraction of one number in terms
of another.
The division is performed with the forward slash /. Here is an example:
SELECT 128 / 42
This would produce 3
When performing the division, be aware of its many rules. Never divide by
zero (0). Make sure that you know the relationship(s) between the numbers
involved in the operation.
|
The Modulo
|
In the above division, 128/42, the result is 3. When
you multiply 42 by 3, as in 42*3, you get 126. In some cases, you may be
interested in knowing the amount that was left out after the operation. The
modulo operation is used to get the remainder of a division as a natural
number. The remainder operation is performed with the percent
sign (%). Here is an example:
SELECT 128 % 42
This would produce 2
|
Additional SQL Operators
|
Parentheses
|
Like most computer languages, you can use parentheses
in a SQL statement to
isolate a group of items that must be considered as belonging to one
entity. For example, as we will learn soon, parentheses allow a function
to delimit the list of its arguments.
Parentheses can also be used to isolate an operation or an expression with
regards to another operation or expression. For example, when studying the
algebraic operations, we saw that the subtraction is not associative and
can lead to unpredictable results. In the same way, if your operation
involves various operators such as a mix of addition(s) and subtraction(s), you can
use parentheses to specify how to proceed with the operations,
that is, what operation should (must) be performed first. Here is an
example:
|
SELECT ((154 - 42) + 8) As Number1; SELECT (154 - (12 + 8)) As Number2; GO |
Number1 ----------- 120 (1 row affected) Number2 ----------- 134 (1 row affected) |
As you can see, using the parentheses controls how the
whole operation would proceed. This difference can be even more
accentuated if your operation includes 3 or more operators and 4 or more
operands.
No comments:
Post a Comment