CAST Function [Data Type Conversion]

Returns the value of an expression converted to a supplied data type.

Syntax

CAST expression AS data type )

Parameters

Parameters

Description

expression

The expression to be converted.

data type

The target data type.

Returns

The specified data type.

Examples

The following function ensures a string is used as a date:

CAST( '2000-10-31' AS DATE )

The value of the expression 1 + 2 is calculated, and the result cast into a single-character string, the length the data server assigns:

CAST( 1 + 2 AS CHAR )

You can use the CAST function to shorten strings:

SELECT CAST( lname AS CHAR(5) ) FROM Customers

Usage

If you do not indicate a length for character string types, Sybase IQ chooses an appropriate length. If neither precision nor scale is specified for a DECIMAL conversion, the database server selects appropriate values.

If neither precision nor scale is specified for the explicit conversion of NULL to NUMERIC, the default is NUMERIC(1,0). For example,
SELECT CAST( NULL AS NUMERIC ) A,
       CAST( NULL AS NUMERIC(15,2) ) B
is described as:
A NUMERIC(1,0)
B NUMERIC(15,2)

Standards and Compatibility

  • SQL—ISO/ANSI SQL compliant.

  • Sybase—Compatible with Adaptive Server Enterprise.

Related reference
CONVERT Function [Data Type Conversion]
HOURS Function [Date and Time]
MINUTES Function [Date and Time]
MONTHS Function [Date and Time]
REPLACE Function [String]
SECOND Function [Date and Time]
WEEKS Function [Date and Time]
YEAR Function [Date and Time]
YEARS Function [Date and Time]
DAYS Function [Date and Time]