Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
9.2. Comparison Functions and Operators
Prev UpChapter 9. Functions and OperatorsHome Next

9.2. Comparison Functions and Operators#

The usual comparison operators are available, as shown inTable 9.1.

Table 9.1. Comparison Operators

OperatorDescription
datatype<datatypebooleanLess than
datatype>datatypebooleanGreater than
datatype<=datatypebooleanLess than or equal to
datatype>=datatypebooleanGreater than or equal to
datatype=datatypebooleanEqual
datatype<>datatypebooleanNot equal
datatype!=datatypebooleanNot equal

Note

<> is the standard SQL notation fornot equal.!= is an alias, which is converted to<> at a very early stage of parsing. Hence, it is not possible to implement!= and<> operators that do different things.

These comparison operators are available for all built-in data types that have a natural ordering, including numeric, string, and date/time types. In addition, arrays, composite types, and ranges can be compared if their component data types are comparable.

It is usually possible to compare values of related data types as well; for exampleinteger>bigint will work. Some cases of this sort are implemented directly bycross-type comparison operators, but if no such operator is available, the parser will coerce the less-general type to the more-general type and apply the latter's comparison operator.

As shown above, all comparison operators are binary operators that return values of typeboolean. Thus, expressions like1 < 2 < 3 are not valid (because there is no< operator to compare a Boolean value with3). Use theBETWEEN predicates shown below to perform range tests.

There are also some comparison predicates, as shown inTable 9.2. These behave much like operators, but have special syntax mandated by the SQL standard.

Table 9.2. Comparison Predicates

Predicate

Description

Example(s)

datatypeBETWEENdatatypeANDdatatypeboolean

Between (inclusive of the range endpoints).

2 BETWEEN 1 AND 3t

2 BETWEEN 3 AND 1f

datatypeNOT BETWEENdatatypeANDdatatypeboolean

Not between (the negation ofBETWEEN).

2 NOT BETWEEN 1 AND 3f

datatypeBETWEEN SYMMETRICdatatypeANDdatatypeboolean

Between, after sorting the two endpoint values.

2 BETWEEN SYMMETRIC 3 AND 1t

datatypeNOT BETWEEN SYMMETRICdatatypeANDdatatypeboolean

Not between, after sorting the two endpoint values.

2 NOT BETWEEN SYMMETRIC 3 AND 1f

datatypeIS DISTINCT FROMdatatypeboolean

Not equal, treating null as a comparable value.

1 IS DISTINCT FROM NULLt (rather thanNULL)

NULL IS DISTINCT FROM NULLf (rather thanNULL)

datatypeIS NOT DISTINCT FROMdatatypeboolean

Equal, treating null as a comparable value.

1 IS NOT DISTINCT FROM NULLf (rather thanNULL)

NULL IS NOT DISTINCT FROM NULLt (rather thanNULL)

datatypeIS NULLboolean

Test whether value is null.

1.5 IS NULLf

datatypeIS NOT NULLboolean

Test whether value is not null.

'null' IS NOT NULLt

datatypeISNULLboolean

Test whether value is null (nonstandard syntax).

datatypeNOTNULLboolean

Test whether value is not null (nonstandard syntax).

booleanIS TRUEboolean

Test whether boolean expression yields true.

true IS TRUEt

NULL::boolean IS TRUEf (rather thanNULL)

booleanIS NOT TRUEboolean

Test whether boolean expression yields false or unknown.

true IS NOT TRUEf

NULL::boolean IS NOT TRUEt (rather thanNULL)

booleanIS FALSEboolean

Test whether boolean expression yields false.

true IS FALSEf

NULL::boolean IS FALSEf (rather thanNULL)

booleanIS NOT FALSEboolean

Test whether boolean expression yields true or unknown.

true IS NOT FALSEt

NULL::boolean IS NOT FALSEt (rather thanNULL)

booleanIS UNKNOWNboolean

Test whether boolean expression yields unknown.

true IS UNKNOWNf

NULL::boolean IS UNKNOWNt (rather thanNULL)

booleanIS NOT UNKNOWNboolean

Test whether boolean expression yields true or false.

true IS NOT UNKNOWNt

NULL::boolean IS NOT UNKNOWNf (rather thanNULL)


TheBETWEEN predicate simplifies range tests:

a BETWEENx ANDy

is equivalent to

a >=x ANDa <=y

Notice thatBETWEEN treats the endpoint values as included in the range.BETWEEN SYMMETRIC is likeBETWEEN 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.

The various variants ofBETWEEN are implemented in terms of the ordinary comparison operators, and therefore will work for any data type(s) that can be compared.

Note

The use ofAND in theBETWEEN syntax creates an ambiguity with the use ofAND as a logical operator. To resolve this, only a limited set of expression types are allowed as the second argument of aBETWEEN clause. If you need to write a more complex sub-expression inBETWEEN, write parentheses around the sub-expression.

Ordinary comparison operators yield null (signifyingunknown), 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 predicates:

a IS DISTINCT FROMba IS NOT DISTINCT FROMb

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 predicates effectively act as though null were a normal data value, rather thanunknown.

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

expression IS NULLexpression IS NOT NULL

or the equivalent, but nonstandard, predicates:

expression ISNULLexpression NOTNULL

Donot writeexpression = NULL becauseNULL is notequal toNULL. (The null value represents an unknown value, and it is not known whether two unknown values are equal.)

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,Postgres Pro will convertx = NULL clauses tox IS NULL.

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; in particular, a row-valued expression that contains both null and non-null fields will return false for both tests. For example:

SELECT ROW(1,2.5,'this is a test') = ROW(1, 3, 'not the same');SELECT ROW(table.*) IS NULL FROM table;  -- detect all-null rowsSELECT ROW(table.*) IS NOT NULL FROM table;  -- detect all-non-null rowsSELECT NOT(ROW(table.*) IS NOT NULL) FROM TABLE; -- detect at least one null in rows

In some cases, it may be preferable to writerowIS DISTINCT FROM NULL orrowIS NOT DISTINCT FROM NULL, which will simply check whether the overall row value is null without any additional tests on the row fields.

Boolean values can also be tested using the predicates

boolean_expression IS TRUEboolean_expression IS NOT TRUEboolean_expression IS FALSEboolean_expression IS NOT FALSEboolean_expression IS UNKNOWNboolean_expression 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 valueunknown. 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.

Some comparison-related functions are also available, as shown inTable 9.3.

Table 9.3. Comparison Functions

Function

Description

Example(s)

num_nonnulls (VARIADIC"any" ) →integer

Returns the number of non-null arguments.

num_nonnulls(1, NULL, 2)2

num_nulls (VARIADIC"any" ) →integer

Returns the number of null arguments.

num_nulls(1, NULL, 2)1



Prev Up Next
9.1. Logical Operators Home 9.3. Mathematical Functions and Operators
pdfepub
Go to Postgres Pro Standard 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp