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 most common expression is a simple column name. You can combine a column name with other expression elements.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.
Table 10-4. Literal Syntax Examples Min Value: 0
Max Value: 1 ‘yyyy-mm-dd’ ‘yyyy-mm-dd hh:mm:ss.SSSSSS' n is the integral partf is the fractional part n is the integral partf is the fractional partx is the exponent ‘hex_value’ ‘value’ ‘hh:mm:ss’ ‘value’ 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.Integer literals are represented by a string of numbers that are not enclosed in quotation marks and do not contain decimal points.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.Binary literals are represented with single quotation marks. The valid characters in a binary literal are 0-9, a-f, and A-F.
■ If an operator is given a null operand, the result is always null. The only operator that does not follow this rule is concatenation (||).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 Denotes a positive or negative expression. These are unary operators. Multiplies, divides. These are binary operators. The concatenation operator manipulates character strings. Table 10-6 lists the only supported concatenation operator.
Table 10-6. Concatenation Operator 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 job IN ('CLERK','ANALYST')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." Tests for existence of rows in a subquery. 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. ESCAPE clause in LIKE operatorLIKE ’pattern string’ ESCAPE ’c’ 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.The default escape character is backslash (\). SELECT * FROM emp WHERE ENAME LIKE 'J%\_%' ESCAPE '\'This matches all records with names that start with letter 'J' and have the '_' character in them.SELECT * FROM emp WHERE ENAME LIKE 'JOE\_JOHN' ESCAPE '\'This matches only records with name ’JOE_JOHN’.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. SELECT * FROM emp WHERE NOT (sal BETWEEN 1000 AND 2000) Returns TRUE if both component conditions are TRUE. Returns FALSE if either is FALSE; otherwise, returns UNKNOWN. SELECT * FROM emp WHERE job = 'CLERK' AND deptno = 10 Returns TRUE if either component condition is TRUE. Returns FALSE if both are FALSE; otherwise, returns UNKNOWN. SELECT * FROM emp WHERE job = 'CLERK' OR deptno = 10In 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: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 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:Because parenthetical expressions are forced to be evaluated first, the OR operation takes precedence over AND.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.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).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.
Table 10-10. Numerical Functions Supported
Table 10-11. String Functions Supported Returns a string where len number of characters beginning at start has been replaced by s2. Returns the leftmost count of characters of s. If s requires double quoting, use SUBSTRING( ) instead. Returns the first index (1=left, 0=not found) where search is found in s, starting at start.
Table 10-12. Date/Time Functions Supported 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:
Table 10-13. System/Connection Functions Supported
Table 10-14. System Functions Supported CONVERT(term,type) Converts term to another data type. CAST(term AS type) Converts term to another data type. 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. When value1 equals value2, then value3 is returned; otherwise, value4 or Null is returned in the absence of ELSE. 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) Any of the date and time terms can be extracted from datetime_value. 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. string_expression is returned from the numeric_expression1 starting location. Optionally, numeric_expression2 specifies the length of the substring. TRIM([{LEADING | TRAILING | BOTH}] FROM string_expression) When returned, either the leading or trailing spaces, or both, are trimmed from string_expression.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