Distinct type comparisons
A value with a distinct type can only be compared to another value with exactly the same type because distinct types have strong typing, which means that a distinct type is compatible only with its own type.
To compare a distinct type to a value with a different data type, the distinct type value must be cast to the data type of the comparison value or the comparison value must be cast to the distinct type. For example, because constants are built-in data types, a constant can be compared to a distinct type value only if it is first cast to the distinct type or vice versa.
The following table shows examples of valid and invalid comparisons,
assuming the following SQL statements were used to define two distinct
types AGE_TYPE and CAMP_DATE and table CAMP_ROSTER table.
CREATE TYPE AGE_TYPE AS INTEGER;
CREATE TYPE CAMP_DATE AS DATE;
CREATE TABLE CAMP_ROSTER
( NAME VARCHAR(20),
ATTENDEE_NUMBER INTEGER NOT NULL,
AGE AGE_TYPE,
FIRST_CAMP_DATE CAMP_DATE,
LAST_CAMP_DATE CAMP_DATE,
BIRTHDATE DATE);
SQL statement | Valid | Reason |
---|---|---|
Distinct types with distinct types | ||
|
Yes | Both values are the same distinct type. |
Distinct types with columns of the same source data type | ||
|
No | A distinct type cannot be compared to integer. |
|
Yes | The distinct type is cast to an integer, making the comparison of two integers. |
|
Yes | Integer ATTENDEE_NUMBER is cast to the distinct type AGE_TYPE, making both values the same distinct type. |
Distinct types with constants | ||
|
No | A distinct type cannot be compared to a constant. |
|
Yes | The distinct type is cast to the data type of constants, making all the values in the comparison integers. |
|
Yes | Constants are cast to distinct type AGE_TYPE, making all the values in the comparison the same distinct type. |
|
No | A distinct type cannot be compared to a constant. |
|
No | The string constant '06/12/99', a VARCHAR data type, cannot be cast directly to distinct type CAMP_DATE, which is based on a DATE data type. As illustrated in the next row, the constant must be cast to a DATE data type and then to the distinct type. |
|
Yes | The string constant '06/12/99' is cast to the distinct type CAMP_DATE, making both values the same distinct type. To cast a string constant to a distinct type that is based on a DATE, TIME, or TIMESTAMP data type, the string constant must first be cast to a DATE, TIME, or TIMESTAMP data type. |
Distinct types with host variables | ||
|
No | The host variables have integer data types. A distinct type cannot be compared to an integer. |
|
Yes | The host variables are cast to distinct type AGE_TYPE, making all the values the same distinct type. |
|
No | The host variable has a VARCHAR data type. A distinct type cannot be compared to a VARCHAR. |
|
Yes | The host variable is cast to the distinct type CAMP_DATE, making both values the same distinct type. To cast a VARCHAR host variable to a distinct type that is based on a DATE, TIME, or TIMESTAMP data type, the host variable must first be cast to a DATE, TIME, or TIMESTAMP data type. |