UDT-to-Numeric Conversion
Purpose
Converts a UDT expression to a numeric data type.
CAST Syntax
where:
Syntax element … |
Specifies … |
UDT_expression |
an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 1318. |
numeric_data_definition |
the target predefined numeric type followed by any optional FORMAT, NAMED, or TITLE attribute phrases. |
ANSI Compliance
This is ANSI SQL:2011 compliant.
As an extension to ANSI, CAST permits the use of data attribute phrases such as FORMAT.
Teradata Conversion Syntax
where:
Syntax element … |
Specifies … |
UDT_expression |
an expression that results in a UDT data type. For details on expressions that can result in UDT data types, see “SQL UDF” on page 1318. |
data_attribute |
one of the following optional data attributes: |
numeric_data_type |
a predefined numeric type to which UDT_expression is to be converted. |
ANSI Compliance
This is a Teradata extension to the ANSI SQL:2011 standard.
Usage Notes
Data type conversions involving UDTs require appropriate cast definitions for the UDTs. To define a cast for a UDT, use the CREATE CAST statement. For more information on CREATE CAST, see SQL Data Definition Language.
Implicit Type Conversion
Teradata Database performs implicit UDT-to-numeric conversions for the following operations:
Performing an implicit data type conversion requires that an appropriate cast definition (see “Usage Notes” on page 766) exists that specifies the AS ASSIGNMENT clause.
The target numeric type of the cast definition does not have to be an exact match to the target numeric type of the implicit conversion. Teradata Database can use an implicit cast definition that specifies a BYTEINT, SMALLINT, INTEGER, BIGINT, DECIMAL/NUMERIC, NUMBER, or REAL/FLOAT/DOUBLE target type.
If multiple implicit cast definitions exist for converting the UDT to different numeric types, Teradata Database uses the implicit cast definition for the numeric type with the highest precedence. The following list shows the precedence of numeric types in order from lowest to highest precedence:
If no UDT-to-numeric implicit cast definitions exist, Teradata Database looks for other cast definitions that can substitute for the UDT-to-numeric implicit cast definition:
IF the following combination of implicit cast definitions exists … |
THEN Teradata Database … |
|
UDT-to- |
UDT-to-Character |
|
X |
|
uses the UDT-to-DATE implicit cast definition. |
|
X |
uses the UDT-to-character implicit cast definition. The character type cannot be CLOB. If multiple UDT-to-character implicit cast definitions exist, then Teradata Database returns an SQL error. |
X |
X |
reports an error. |
Substitutions are valid because Teradata Database can use the implicit cast definition to cast the UDT to the substitute data type, and then implicitly cast the substitute data type to a numeric type.
Example
Consider the following table definition, where euro is a UDT:
CREATE TABLE euro_sales_table
(quarter INTEGER
,region VARCHAR(20)
,sales euro );
Assuming an appropriate cast definition exists for the euro UDT, the following statement converts the values in the sales column to DECIMAL(10,2):
SELECT SUM (CAST (sales AS DECIMAL(10,2))) FROM euro_sales_table;
Related Topics
For details on data types and data attributes, see SQL Data Types and Literals.