SQLite Code Factory online Help
Prev | Return to chapter overview | Next |
EXPRESSION
expr ::= |
expr binary-op expr | expr like-op expr | unary-op expr | ( expr ) | column-name | table-name . column-name | literal-value | function-name ( expr-list | * ) | expr ISNULL | expr NOTNULL | expr [NOT] BETWEEN expr AND expr | expr [NOT] IN ( value-list ) | expr [NOT] IN ( select-statement ) | ( select-statement ) | CASE [expr] ( WHEN expr THEN expr )+ [ELSE expr] END |
like-op ::= |
LIKE | GLOB | NOT LIKE | NOT GLOB |
This section is different from the others. Most other sections of this document talks about a particular SQL command. This section does not talk about a standalone command but about "expressions" which are subcomponent of most other commands.
SQLite understands the following binary operators, in order from highest to lowest precedence:
||
* / %
+ -
<< >> & |
< <= > >=
= == != <> IN
AND
OR
Supported unary operators are these:
- + ! ~
Any SQLite value can be used as part of an expression. For arithmetic operations, integers are treated as integers. Strings are first converted to real numbers using atof(). For comparison operators, numbers compare as numbers and strings compare using the strcmp() function. Note that there are two variations of the equals and not equals operators. Equals can be either = or ==. The non-equals operator can be either != or <>. The || operator is "concatenate" - it joins together the two strings of its operands.
The LIKE operator does a wildcard comparison. The operand to the right contains the wildcards. A percent symbol % in the right operand matches any sequence of zero or more characters on the left. An underscore _ on the right matches any single character on the left. The LIKE operator is not case sensitive and will match upper case characters on one side against lower case characters on the other. (A bug: SQLite only understands upper/lower case for 7-bit Latin characters. Hence the LIKE operator is case sensitive for 8-bit iso8859 characters or UTF-8 characters. For example, the expression 'a' LIKE 'A' is TRUE but 'æ' LIKE 'Æ' is FALSE.)
The GLOB operator is similar to LIKE but uses the Unix file globing syntax for its wildcards. Also, GLOB is case sensitive, unlike LIKE. Both GLOB and LIKE may be preceded by the NOT keyword to invert the sense of the test.
A column name can be any of the names defined in the CREATE TABLE statement or one of the following special identifiers: "ROWID", "OID", or "_ROWID_". These special identifiers all describe the unique random integer key (the "row key") associated with every row of every table. The special identifiers only refer to the row key if the CREATE TABLE statement does not define a real column with the same name. Row keys act like read-only columns. A row key can be used anywhere a regular column can be used, except that you cannot change the value of a row key in an UPDATE or INSERT statement. "SELECT * ..." does not return the row key.
SELECT statements can appear in expressions as either the right-hand operand of the IN operator or as a scalar quantity. In both cases, the SELECT should have only a single column in its result. Compound SELECTs (connected with keywords like UNION or EXCEPT) are allowed. A SELECT in an expression is evaluated once before any other processing is performed, so none of the expressions within the select itself can refer to quantities in the containing expression.
When a SELECT is the right operand of the IN operator, the IN operator returns TRUE if the result of the left operand is any of the values generated by the select. The IN operator may be preceded by the NOT keyword to invert the sense of the test.
When a SELECT appears within an expression but is not the right operand of an IN operator, then the first row of the result of the SELECT becomes the value used in the expression. If the SELECT yields more than one result row, all rows after the first are ignored. If the SELECT yields no rows, then the value of the SELECT is NULL.
Both simple and aggregate functions are supported. A simple function can be used in any expression. Simple functions return a result immediately based on their inputs. Aggregate functions may only be used in a SELECT statement. Aggregate functions compute their result across all rows of the result set.
The functions shown below are available by default. Additional
abs(X) |
Return the absolute value of argument X. |
coalesce(X,Y,...) |
Return a copy of the first non-NULL argument. If all arguments are NULL then NULL is returned. |
glob(X,Y) |
This function is used to implement the "Y GLOB X" syntax of SQLite. |
last_insert_rowid() |
Return the ROWID of the last row insert from this connection to the database. This is the same value that would be returned from the |
length(X) |
Return the string length of X in characters. If SQLite is configured to support UTF-8, then the number of UTF-8 characters is returned, not the number of bytes. |
like(X,Y) |
This function is used to implement the "Y LIKE X" syntax of SQL. |
lower(X) |
Return a copy of string X will all characters converted to lower case. The C library tolower() routine is used for the conversion, which means that this function might not work correctly on UTF-8 characters. |
max(X,Y,...) |
Return the argument with the maximum value. Arguments may be strings in addition to numbers. The maximum value is determined by the usual sort order. Note that max() is a simple function when it has 2 or more arguments but converts to an aggregate function if given only a single argument. |
min(X,Y,...) |
Return the argument with the minimum value. Arguments may be strings in addition to numbers. The minimum value is determined by the usual sort order. Note that min() is a simple function when it has 2 or more arguments but converts to an aggregate function if given only a single argument. |
random(*) |
Return a random integer between -2147483648 and +2147483647. |
round(X) round(X,Y) |
Round off the number X to Y digits to the right of the decimal point. If the Y argument is omitted, 0 is assumed. |
substr(X,Y,Z) |
Return a substring of input string X that begins with the Y-th character and which is Z characters long. The left-most character of X is number 1. If Y is negative the first character of the substring is found by counting from the right rather than the left. If SQLite is configured to support UTF-8, then characters indices refer to actual UTF-8 characters, not bytes. |
upper(X) |
Return a copy of input string X converted to all upper-case letters. |
avg(X) |
Return the average value of all X within a group. |
count(X) count(*) |
The first form return a count of the number of times that X is not NULL in a group. The second form (with no argument) returns the total number of rows in the group. |
max(X) |
Return the maximum value of all values in the group. The usual sort order is used to determine the maximum. |
min(X) |
Return the minimum value of all values in the group. The usual sort order is used to determine the minimum. |
sum(X) |
Return the numeric sum of all values in the group. |
Prev | Return to chapter overview | Next |