Datatype conversion functions

Datatype conversion functions change expressions from one datatype to another and specify new display formats for date/time information. The datatype conversion functions are:

The datatype conversion functions can be used in the select list, in the where clause, and anywhere else an expression is allowed.

Adaptive Server performs certain datatype conversions automatically. These are called implicit conversions. For example, if you compare a char expression and a datetime expression, or a smallint expression and an int expression, or char expressions of different lengths, Adaptive Server automatically converts one datatype to another.

You must request other datatype conversions explicitly, using one of the built-in datatype conversion functions. For example, before concatenating numeric expressions, you must convert them to character expressions.

Adaptive Server does not allow you to convert certain datatypes to certain other datatypes, either implicitly or explicitly. For example, you cannot convert the following:

Unsupported conversions result in error messages.

Table 2-3 indicates whether individual datatype conversions are performed implicitly, explicitly, or are not supported.

Table 2-3: Explicit, implicit, and unsupported datatype conversions

From

tinyint

smallint

int

decimal

numeric

float

real

[n]char

[n]varchar

unichar

univarchar

text

smallmoney

money

bit

smalldatetime

datetime

binary

varbinary

image

Key:

  • E – explicit datatype conversion is required.

  • I – conversion can be done either implicitly, or with an explicit datatype conversion function.

  • I/E – Explicit datatype conversion function required when there is loss of precision or scale, and arithabortnumeric_truncation is on; implicit conversion allowed otherwise.

  • U – unsupported conversion.

  • – Conversion of a datatype to itself. These conversions are allowed, but are meaningless.

tinyint

I

I

I

I

I

I

E

E

E

E

U

I

I

I

U

U

I

I

U

smallint

I

I

I

I

I

I

E

E

E

E

U

I

I

I

U

U

I

I

U

int

I

I

I

I

I

I

E

E

E

E

U

I

I

I

U

U

I

I

U

decimal

I

I

I

I/E

I/E

I

I

E

E

E

E

U

I

I

I

U

U

I

I

U

numeric

I

I

I

I/E

I/E

I

I

E

E

E

E

U

I

I

I

U

U

I

I

U

real

I

I

I

I

I

I

E

E

E

E

U

I

I

I

U

U

I

I

U

float

I

I

I

I

I

I

E

E

E

E

U

I

I

I

U

U

I

I

U

[n]char

E

E

E

E

E

E

E

I

I

I

I

I

E

E

E

I

I

I

I

I

[n]varchar

E

E

E

E

E

E

E

I

I

I

I

I

E

E

E

I

I

I

I

I

unichar

E

E

E

E

E

E

E

I

I

I

I

E

E

E

I

I

I

I

I

univarchar

E

E

E

E

E

E

E

I

I

I

I

E

E

E

I

I

I

I

I

text

U

U

U

U

U

U

U

E

E

E

E

U

U

U

U

U

U

U

U

U

smallmoney

I

I

I

I

I

I

I

I

I

E

E

U

I

I

U

U

I

I

U

money

I

I

I

I

I

I

I

I

I

E

E

U

I

I

U

U

I

I

U

bit

I

I

I

I

I

I

I

I

I

E

E

U

I

I

U

U

I

I

U

smalldatetime

U

U

U

U

U

U

U

E

E

I

I

U

U

U

U

I

I

I

U

datetime

U

U

U

U

U

U

U

E

E

I

I

U

U

U

U

U

I

I

U

binary

I

I

I

I

I

I

I

I

I

E

E

U

I

I

I

U

U

I

I

varbinary

I

I

I

I

I

I

I

I

I

E

E

U

I

I

I

U

U

I

I

image

U

U

U

U

U

U

U

U

U

E

E

U

U

U

U

U

U

I

I

U