August 15th, 2008

Arithmetic Operations, Part 2 of 2

Posted by admin in C. SELECT Statement

Column names can be intermixed with arithmetic symbols to produce an arithmetic result.

SELECT column1 arithoper1 column2,
       column3 arithoper2 column4, ...
  FROM table1, table2, ...;

> Valid expression symbols are: + (addition), – (subtraction), * (multiplication), / (division).
> If any operand contains the NULL value, the entire calculation evaluates as NULL.
> The columns and operands used within the expression must be compatible data types.
> If operands are mixed numeric data-types, the ANSI/ISO standard specifies automatic data-type conversion from integers to decimal numbers, and from decimal numbers to floating point numbers as required. You can mix these data types in a numeric expression.
> DB2 also uses the || symbol to concatenate, or combine two fields into one.

August 14th, 2008

Arithmetic Operations, Part 1 of 2

Posted by admin in C. SELECT Statement

An operator acts on one or more elements. Arithmetic operators give a mathematical result.

-expression                Reverses the sign of an expression.
+expression                Leaves the expression unchanged.
expression1 + expression2  Adds expression1 and expression2.
expression1 - expression2  Subtracts expression2 from expression1.
expression1 * expression2  Multiplies expression1 and expression2.
expression1 / expression2  Divides expression1 by expression2.

Operators with higher precedence are evaluated first. Arithmetic operators (+, -, *, /, etc.) are evaluated before comparison operators (<, =, >, etc.), which are evaluated before logical operators (AND, OR, NOT). You may have to use parentheses to control the calculation order. You may recall having learned the mathematical order of operations.

A * B < C OR D

is the same as

((A * B) < C) OR D 

A function is a named routine that performs a task on an argument passed to it in parentheses.

While these are the arithmetic operators required by the SQL standards, RDBMS implementations by various providers include many functions and operators beyond these basic functions for scientific, trigonometric, financial, statistical, mathematical, concatenation, string, conversion, system, date/time, bit manipulation, security, and other functions.

August 13th, 2008

Conditions and Comparisons

Posted by admin in C. SELECT Statement

There are five types of conditions in SQL:

Operators           Conditions       
=, <>, <, >, <=, >= Comparison
IS NULL             Null testing
BETWEEN             Range of values
IN                  In a list
LIKE                Pattern matching

IS NULL, BETWEEN, IN, and LIKE will be covered later.

There are six logical comparison operators or predicates in SQL:

Operator  Comparison                Expression       
=         Equal to                  MAX_THRUST = 500
<>        Not equal to              MAX_THRUST <> 500
<         Less than                 MAX_THRUST < 500
>         Greater than              MAX_THRUST > 500
<=        Less than or equal to     MAX_THRUST <= 500
>=        Greater than or equal to  MAX_THRUST >= 500

Notice that “=<” and “=>”are invalid. Only use “<=” and “>=”.

Combine and negate with:

AND   Both conditions are true
OR     Either condition is true
NOT   Reverse a condition

« Previous PageNext Page »