Comparison Functions

Confluent Cloud for Apache Flink®️ provides these built-in comparison functions to use in SQL queries:

Equality operations

SQL function Description
value1 = value2 Returns TRUE if value1 is equal to value2. Returns UNKNOWN if value1 or value2 is NULL.
value1 <> value2 Returns TRUE if value1 is not equal to value2. Returns UNKNOWN if value1 or value2 is NULL.
value1 > value2 Returns TRUE if value1 is greater than value2. Returns UNKNOWN if value1 or value2 is NULL.
value1 >= value2 Returns TRUE if value1 is greater than or equal to value2. Returns UNKNOWN if value1 or value2 is NULL.
value1 < value2 Returns TRUE if value1 is less than value2. Returns UNKNOWN if value1 or value2 is NULL.
value1 <= value2 Returns TRUE if value1 is less than or equal to value2. Returns UNKNOWN if value1 or value2 is NULL.

Logical operations

Logical operation Description
boolean1 OR boolean2 Returns TRUE if boolean1 is TRUE or boolean2 is TRUE. Supports three-valued logic. For example, TRUE || NULL(BOOLEAN) returns TRUE.
boolean1 AND boolean2 Returns TRUE if boolean1 and boolean2 are both TRUE. Supports three-valued logic. For example, TRUE && NULL(BOOLEAN) returns UNKNOWN.
NOT boolean Returns TRUE if boolean is FALSE; returns FALSE if boolean is TRUE; returns UNKNOWN if boolean is UNKNOWN.
boolean IS FALSE Returns TRUE if boolean is FALSE; returns FALSE if boolean is TRUE or UNKNOWN.
boolean IS NOT FALSE Returns TRUE if boolean is TRUE or UNKNOWN; returns FALSE if boolean is FALSE.
boolean IS TRUE Returns TRUE if boolean is TRUE; returns FALSE if boolean is FALSE or UNKNOWN.
boolean IS NOT TRUE Returns TRUE if boolean is FALSE or UNKNOWN; returns FALSE if boolean is TRUE.
boolean IS UNKNOWN Returns TRUE if boolean is UNKNOWN; returns FALSE if boolean is TRUE or FALSE.
boolean IS NOT UNKNOWN Returns TRUE if boolean is TRUE or FALSE; returns FALSE if boolean is UNKNOWN.

Conversion functions

CAST

Casts a value to a different type.

Syntax
CAST(value AS type)
Description

The CAST function returns the specified value cast to the type specified by type.

A cast error throws an exception and fails the job.

When performing a cast operation that may fail, like STRING to INT, prefer TRY_CAST, to enable handling errors.

If table.exec.legacy-cast-behaviour is enabled, the CAST function behaves like TRY_CAST.

Examples
--  returns 42
SELECT CAST('42' AS INT);

-- returns NULL of type STRING
SELECT CAST(NULL AS STRING);

--  throws an exception and fails the job
SELECT CAST('not-a-number' AS INT);

TRY_CAST

Casts a value to a different type and returns NULL on error.

Syntax
TRY_CAST(value AS type)
Description
Similar to the CAST function, but in case of error, returns NULL rather than failing the job.
Examples
--  returns 42
SELECT TRY_CAST('42' AS INT);

--  returns NULL of type STRING
SELECT TRY_CAST(NULL AS STRING);

--  returns NULL of type INT
SELECT TRY_CAST('not-a-number' AS INT);

--  returns 0 of type INT
SELECT COALESCE(TRY_CAST('not-a-number' AS INT), 0);

TYPEOF

Gets the string representation of a data type.

Syntax
TYPEOF(input)
TYPEOF(input, force_serializable)
Description

The TYPEOF function returns the string representation of the input expression’s data type.

By default, the returned string is a summary string that might omit certain details for readability.

If force_serializable is set to TRUE, the string represents a full data type that can be persisted in a catalog.

Anonymous, inline data types have no serializable string representation. In these cases, NULL is returned.

Examples
-- returns "CHAR(13) NOT NULL"
SELECT TYPEOF('a string type');

-- returns "INT NOT NULL"
SELECT TYPEOF(23);

-- returns "DATE NOT NULL"
SELECT TYPEOF(DATE '2023-05-04');

-- returns "NULL"
SELECT TYPEOF(NULL);