Unary operators are the simplest type of expression operator. They take a single (or unitary) parameter expression and modify or alter that expression in some way. In all cases, if the parameter expression is NULL, the operator will also return NULL.
SQLite supports the following unary expression operators:
-
Sign negationA unary negative sign will invert the sign of a numeric expression, and is equivalent to being multiplied by ‒1. Positive expressions become negative, while negative expressions become positive. Any non-NULL parameter expression will be converted into a numeric type before the conversion.
+
Positive signLogically, this operator is a nonoperation. It does not force
numbers to be positive (use the abs()
SQL function for that), it simply
maintains the current sign. It can be used with any
datatype, including text and BLOB types, and will simply
return the value, without conversion.
Although this operator does not alter the value of the parameter expression, the result expression is still considered a “computed” expression. Applying this operator to a column identifier will dissociate the resulting expression from the source table. This alters the way the query optimizer considers the expression. For example, the optimizer won’t attempt to use any indexes associated with the source column or a computed result column.
~
Bit inversionInverts or negates all of the bits of the parameter expression. Any non-NULL parameter expression will be converted into an integer before the bit inversion.
NOT
Logic inversionThe NOT
operator is used to invert the meaning of any logic
expression. Any non-NULL expression will be converted to
an integer value. All nonzero values will return 0, while
a 0 value will return 1. Don’t confuse this unary operator
with the optional NOT
found in some binary operators. The end result is the
same, but the syntax ordering is a bit different.
Along with the COLLATE
expression, these operators have the highest
precedence.