10 SQL Statements and Extensions for the Salesforce Driver : SQL Expressions

SQL Expressions
An expression is a combination of one or more values, operators, and SQL functions that evaluate to a value. You can use expressions in the Where, Having, and Order By clauses of Select statements; and in the Set clauses of Update statements.
Expressions enable you to use mathematical operations as well as character string manipulation operators to form complex queries.
The Salesforce driver supports both unquoted and quoted identifiers. An unquoted identifier must start with an ASCII alpha character and can be followed by zero or more ASCII alphanumeric characters. Unquoted identifiers are converted to uppercase before being used.
Quoted identifiers must be enclosed in double quotation marks (""). A quoted identifier can contain any Unicode character including the space character. The Salesforce driver recognizes the Unicode escape sequence \uxxxx as a Unicode character. You can specify a double quotation mark in a quoted identifier by escaping it with a double quotation mark.
The maximum length of both quoted and unquoted identifiers is 128 characters.
Valid expression elements are:
Column Names
The most common expression is a simple column name. You can combine a column name with other expression elements.
Literals
Literals are fixed data values. For example, in the expression PRICE * 1.05, the value 1.05 is a constant. Literals are classified into types, including the following:
Table 10-4 describes the literal format for supported SQL data types.
n
where n is any valid integer value in the range of the INTEGER data type
Min Value: 0
Max Value: 1
yyyy-mm-dd
yyyy-mm-dd hh:mm:ss.SSSSSS'
n is the integral part
f is the fractional part
n.fEx
n is the integral part
f is the fractional part
x is the exponent
1.2E0 or 2.5E40 or -3.45E2 or 5.67E-4
n
where n is a valid integer value in the range of the INTEGER data type
12 or -34 or 0
hex_value
value
hh:mm:ss
value
Character String Literals
Text specifies a character string literal. A character string literal must be enclosed in single quotation marks. To represent one single quotation mark within a literal, you must enter two single quotation marks. When the data in the fields is returned to the client, trailing blanks are stripped.
A character string literal can have a maximum length of 32 KB, that is, (32*1024) bytes.
Examples
'Hello'
'Jim''s friend is Joe'
Integer Literals
Integer literals are represented by a string of numbers that are not enclosed in quotation marks and do not contain decimal points.
NOTE: Integer constants must be whole numbers; they cannot contain decimals.
Optionally, the integer literals can start with sign characters (+/-)
Examples
1994
-2
Numeric Literals
Unquoted numeric values are treated as numeric literals. If the unquoted numeric value contains a decimal point or exponent, it is treated as a real literal; otherwise, it is treated as an integer literal.
Example
+1894.1204
Binary Literals
Binary literals are represented with single quotation marks. The valid characters in a binary literal are 0-9, a-f, and A-F.
Example
'00af123d'
Date/Time Literals
Date and time literal values are:
Operators
This section describes the operators that can be used in SQL expressions.
Unary Operator
A unary operator operates on only one operand.
Grammar
operator operand
Binary Operator
A binary operator operates on two operands.
Grammar
operand1 operator operand2
If an operator is given a null operand, the result is always null. The only operator that does not follow this rule is concatenation (||).
Arithmetic Operators
You can use an arithmetic operator in an expression to negate, add, subtract, multiply, and divide numeric values. The result of this operation is also a numeric value. The + and - operators are also supported in date/time fields to allow date arithmetic. Table 10-5 lists the supported arithmetic operators.
Table 10-5. Arithmetic Operators 
Concatenation Operator
The concatenation operator manipulates character strings. Table 10-6 lists the only supported concatenation operator.
The result of concatenating two character strings is the data type VARCHAR.
Comparison Operators
Comparison operators compare one expression to another. The result of such a comparison can be TRUE, FALSE, or UNKNOWN (if one of the operands is NULL). The Salesforce driver considers the UNKNOWN result as FALSE.
Table 10-7 lists the supported comparison operators.
Table 10-7. Comparison Operators 
!=
<>
>
<
>=
<=
“Greater than or equal to" and "less than or equal to" tests.
SELECT * FROM emp WHERE sal IN (SELECT sal FROM emp WHERE deptno = 30)
"Greater than or equal to x" and "less than or equal to y."
SELECT empno, ename, deptno FROM emp e WHERE EXISTS (SELECT deptno FROM dept WHERE e.deptno = dept.deptno)
Tests whether the value of the column or expression is NULL.
The Escape clause is supported in the LIKE predicate to indicate the escape character. Escape characters are used in the pattern string to indicate that any wildcard character that is after the escape character in the pattern string should be treated as a regular character.
This matches all records with names that start with letter 'J' and have the '_' character in them.
Logical Operators
A logical operator combines the results of two component conditions to produce a single result or to invert the result of a single condition. Table 10-8 lists the supported logical operators.
Table 10-8. Logical Operators 
Returns TRUE if the following condition is FALSE. Returns FALSE if it is TRUE. If it is UNKNOWN, it remains UNKNOWN.
Returns TRUE if both component conditions are TRUE. Returns FALSE if either is FALSE; otherwise, returns UNKNOWN.
Returns TRUE if either component condition is TRUE. Returns FALSE if both are FALSE; otherwise, returns UNKNOWN.
Example
In the Where clause of the following Select statement, the AND logical operator is used to ensure that managers earning more than $1000 a month are returned in the result:
SELECT * FROM emp WHERE jobtitle = manager AND sal > 1000
Operator Precedence
As expressions become more complex, the order in which the expressions are evaluated becomes important. Table 10-9 shows the order in which the operators are evaluated. The operators in the first line are evaluated first, then those in the second line, and so on. Operators in the same line are evaluated left to right in the expression.You can change the order of precedence by using parentheses. Enclosing expressions in parentheses forces them to be evaluated together.
Table 10-9. Operator Precedence 
Examples
Example A
The query in the following example returns employee records for which the department number is 1 or 2 and the salary is greater than $1000:
SELECT * FROM emp WHERE (deptno = 1 OR deptno = 2) AND sal > 1000
Because parenthetical expressions are forced to be evaluated first, the OR operation takes precedence over AND.
Example B
In the following example, the query returns records for all the employees in department 1, but only employees whose salary is greater than $1000 in department 2.
SELECT * FROM emp WHERE deptno = 1 OR deptno = 2 AND sal > 1000
The AND operator takes precedence over OR, so that the search condition in the example is equivalent to the expression deptno = 1 OR (deptno = 2 AND sal > 1000).
Functions
The Salesforce driver supports a number of functions that you can use in expressions, as listed and described in Table 10-10 through Table 10-14.
 
Returns the count of units of time elapsed from datetime1 to datetime2. The string indicates the unit of time and can have the following values:
 
IFNULL(expr,value)
If expr is NULL, then value is returned; otherwise the result of expr is returned. See COALESCE(expr1, expr2, ...) for evaluating multiple expressions.
CONVERT(term,type)
Converts term to another data type.
CAST(term AS type)
Converts term to another data type.
COALESCE(expr1, expr2, ...)
If expr1 is not Null, then it is returned; otherwise, expr2 is evaluated and, if not Null, it is returned, and so on. This is an ANSISQL standard system function.
NULLIF(value1,value2)
If value1 equals value2, then Null is returned; otherwise, value1 is returned.
CASE value1 WHEN value2 THEN value3 [ELSE value4] END
When value1 equals value2, then value3 is returned; otherwise, value4 or Null is returned in the absence of ELSE.
CASE WHEN expr1 THEN value1 [WHEN expr2 THEN value2] [ELSE value4] END
When expr1 is true, then value1 is returned (optionally repeated for more cases); otherwise value4 or Null is returned in the absence of ELSE.
EXTRACT ({YEAR | MONTH | DAY | HOUR | MINUTE| SECOND} FROM datetime_value)
POSITION(string_expression1 IN string_expression2)
If string_expression1 is a sub-string of string_expression2, then the position of the sub-string, counting from one, is returned; otherwise, 0 is returned.
SUBSTRING(string_expression FROM numeric_expression1 [FOR numeric_expression2])
string_expression is returned from the numeric_expression1 starting location. Optionally, numeric_expression2 specifies the length of the substring.
Conditions
A condition specifies a combination of one or more expressions and logical operators that evaluates to either TRUE, FALSE, or UNKNOWN. You can use a condition in the Where clause of the Delete, Select, and Update statements; and in the Having clauses of Select statements. Table 10-15 describes supported conditions.
 
Table 10-15. Conditions