Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
PostgreSQL 9.4.1 Documentation
PrevUpChapter 9. Functions and OperatorsNext

9.2. Comparison Operators

The usual comparison operators are available, shown inTable 9-1.

Table 9-1. Comparison Operators

OperatorDescription
<less than
>greater than
<=less than or equal to
>=greater than or equal to
=equal
<> or!=not equal

Note: The!= operator is converted to<> in the parser stage. It is not possible to implement!= and<> operators that do different things.

Comparison operators are available for all relevant data types. All comparison operators are binary operators that return values of typeboolean; expressions like1 < 2 < 3 are not valid (because there is no< operator to compare a Boolean value with3).

In addition to the comparison operators, the specialBETWEEN construct is available:

a BETWEENx ANDy

is equivalent to

a >=x ANDa <=y

Notice thatBETWEEN treats the endpoint values as included in the range.NOT BETWEEN does the opposite comparison:

a NOT BETWEENx ANDy

is equivalent to

a <x ORa >y

BETWEEN SYMMETRIC is the same asBETWEEN except there is no requirement that the argument to the left ofAND be less than or equal to the argument on the right. If it is not, those two arguments are automatically swapped, so that a nonempty range is always implied.

To check whether a value is or is not null, use the constructs:

expression IS NULLexpression IS NOT NULL

or the equivalent, but nonstandard, constructs:

expression ISNULLexpression NOTNULL

Donot writeexpression = NULL becauseNULL is not"equal to"NULL. (The null value represents an unknown value, and it is not known whether two unknown values are equal.) This behavior conforms to the SQL standard.

Tip: Some applications might expect thatexpression = NULL returns true ifexpression evaluates to the null value. It is highly recommended that these applications be modified to comply with the SQL standard. However, if that cannot be done thetransform_null_equals configuration variable is available. If it is enabled,PostgreSQL will convertx = NULL clauses tox IS NULL.

Note: If theexpression is row-valued, thenIS NULL is true when the row expression itself is null or when all the row's fields are null, whileIS NOT NULL is true when the row expression itself is non-null and all the row's fields are non-null. Because of this behavior,IS NULL andIS NOT NULL do not always return inverse results for row-valued expressions, i.e., a row-valued expression that contains both NULL and non-null values will return false for both tests. This definition conforms to the SQL standard, and is a change from the inconsistent behavior exhibited byPostgreSQL versions prior to 8.2.

Ordinary comparison operators yield null (signifying"unknown"), not true or false, when either input is null. For example,7 = NULL yields null, as does7 <> NULL. When this behavior is not suitable, use theIS [ NOT] DISTINCT FROM constructs:

expression IS DISTINCT FROMexpressionexpression IS NOT DISTINCT FROMexpression

For non-null inputs,IS DISTINCT FROM is the same as the<> operator. However, if both inputs are null it returns false, and if only one input is null it returns true. Similarly,IS NOT DISTINCT FROM is identical to= for non-null inputs, but it returns true when both inputs are null, and false when only one input is null. Thus, these constructs effectively act as though null were a normal data value, rather than"unknown".

Boolean values can also be tested using the constructs

expression IS TRUEexpression IS NOT TRUEexpression IS FALSEexpression IS NOT FALSEexpression IS UNKNOWNexpression IS NOT UNKNOWN

These will always return true or false, never a null value, even when the operand is null. A null input is treated as the logical value"unknown". Notice thatIS UNKNOWN andIS NOT UNKNOWN are effectively the same asIS NULL andIS NOT NULL, respectively, except that the input expression must be of Boolean type.


PrevHomeNext
Logical OperatorsUpMathematical Functions and Operators
Go to PostgreSQL 9.4
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp