- Categories:
[ NOT ] BETWEEN¶
Returns TRUE when the input expression (numeric or string) is within the specified lower and upper boundary.
Syntax¶
Arguments¶
exprThe input expression.
lower_boundThe lower boundary.
upper_boundThe upper boundary.
Returns¶
The function returns a value of type BOOLEAN.
Usage notes¶
expr BETWEEN lower_bound AND upper_boundis equivalent toexpr >= lower_bound AND expr <= upper_bound.The data types of the argument values must be the same or compatible.
If the function implicitly casts a value to a different data type, it might return unexpected results.
For example, when
expris a TIMESTAMP value, and thelower_boundandupper_boundvalues are DATE values, the DATE values are implicitly cast to TIMESTAMP values, and the time is set to00:00:00. For the following WHERE clause, assumetimestamp_columnis a column of type TIMESTAMP in a table:When the DATE values are implicitly cast, the WHERE clause is interpreted as the following:
With this WHERE clause, the function returns
FALSEfor virtually alltimestamp_columnvalues on 2025-04-31, which might not be intended. To avoid this specific issue, you can specify the next day forupper_boundwhen you call the function:
Collation details¶
The expression A BETWEEN X AND Y is equivalent to A >= X AND A <= Y. The collations used for comparing
with X and Y are independent and do not need to be identical, but both need to be compatible with the
collation of A.
Examples¶
Here are a few simple examples of using BETWEEN with numeric and string values:
The following examples use COLLATE with BETWEEN: