Datatype conversion functions change expressions from one datatype to another and specify new display formats for date and time information. The datatype conversion functions are:
You can use the datatype conversion functions 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:
smallint data to datetime
datetime data to smallint
binary or varbinary data to smalldatetime or datetime data
Unsupported conversions result in error messages.
Implicit conversion are allowed between new and existing chronological datatypes.
Implicit conversion between types whose primary fields do not match may cause either data truncation, the insertion of a default value, or an error message to be raised. For example, when a bigdatetime value is converted to a date value, the time portion is truncated leaving only the date portion. If a bigtime value is converted to a bigdatetime value, a default date portion of Jan 1, 0001 is added to the new bigdatetime value. If a date value is converted to a bigdatetime value, a default time portion of 00:00:00.000000 is added to the bigdatetime value.
Implicit and explicit conversions are allowed where a decreased precision results in the loss of data.
Table 2-3 and Table 2-4 indicate whether individual datatype conversions are performed implicitly, explicitly, or are not supported.
From |
binary |
varbinary |
bit |
[n]char |
[n]varchar |
datetime |
smalldatetime |
bigdatetime |
bigtime |
tinyint |
smallint |
unsigned smallint |
int |
unsigned int |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
binary |
– |
I |
I |
I |
I |
U |
U |
I |
I |
I |
I |
I |
I |
I |
varbinary |
I |
– |
I |
I |
I |
U |
U |
I |
I |
I |
I |
I |
I |
I |
bit |
I |
I |
– |
I |
I |
U |
U |
U |
U |
I |
I |
I |
I |
I |
[n]char |
I |
I |
E |
– |
I |
I |
I |
I |
I |
E |
E |
E |
E |
E |
[n]varchar |
I |
I |
E |
I |
– |
I |
I |
I |
I |
E |
E |
E |
E |
E |
datetime |
I |
I |
U |
I |
I |
– |
I |
I |
I |
U |
U |
U |
U |
U |
smalldatetime |
I |
I |
U |
I |
I |
I |
– |
I |
I |
U |
U |
U |
U |
U |
bigdatetime |
I |
I |
U |
I |
I |
I |
I |
- |
I |
U |
U |
U |
U |
U |
bigtime |
I |
I |
U |
I |
I |
I |
I |
I |
- |
U |
U |
U |
U |
U |
tinyint |
I |
I |
I |
E |
E |
U |
U |
U |
U |
– |
I |
I |
I |
I |
smallint |
I |
I |
I |
E |
E |
U |
U |
U |
U |
I |
– |
I |
I |
I |
unsigned smallint |
I |
I |
I |
E |
E |
U |
U |
U |
U |
I |
I |
– |
I |
I |
int |
I |
I |
I |
E |
E |
U |
U |
U |
U |
I |
I |
I |
– |
I |
unsigned int |
I |
I |
I |
E |
E |
U |
U |
U |
U |
I |
I |
I |
I |
– |
bigint |
I |
I |
I |
E |
E |
U |
U |
U |
U |
I |
I |
I |
I |
I |
unsigned bigint |
I |
I |
I |
E |
E |
U |
U |
U |
I |
I |
I |
I |
I |
|
decimal |
I |
I |
I |
E |
E |
U |
U |
U |
U |
I |
I |
I |
I |
I |
numeric |
I |
I |
I |
E |
E |
U |
U |
U |
U |
I |
I |
I |
I |
I |
float |
I |
I |
I |
E |
E |
U |
U |
U |
U |
I |
I |
I |
I |
I |
real |
I |
I |
I |
E |
E |
U |
U |
U |
U |
I |
I |
I |
I |
I |
money |
I |
I |
I |
I |
I |
U |
U |
U |
U |
I |
I |
I |
I |
I |
smallmoney |
I |
I |
I |
I |
I |
U |
U |
U |
U |
I |
I |
I |
I |
I |
text |
U |
U |
U |
E |
E |
U |
U |
U |
U |
U |
U |
U |
U |
U |
unitext |
E |
E |
E |
E |
E |
U |
U |
U |
U |
U |
U |
U |
U |
U |
image |
E |
E |
U |
U |
U |
U |
U |
U |
U |
U |
U |
U |
U |
U |
unichar |
I |
I |
E |
I |
I |
I |
I |
I |
I |
E |
E |
E |
E |
E |
univarchar |
I |
I |
E |
I |
I |
I |
I |
I |
I |
E |
E |
E |
E |
E |
date |
I |
I |
U |
I |
I |
I |
U |
I |
U |
U |
U |
U |
U |
U |
time |
I |
I |
U |
I |
I |
I |
U |
I |
I |
U |
U |
U |
U |
U |
From |
bigint |
unsigned bigint |
decimal |
numeric |
float |
real |
money |
smallmoney |
text |
unitext |
image |
unichar |
univarchar |
date |
time |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
binary |
I |
I |
I |
I |
I |
I |
I |
I |
U |
I |
I |
I |
I |
I |
I |
varbinary |
I |
I |
I |
I |
I |
I |
I |
I |
U |
I |
I |
I |
I |
I |
I |
bit |
I |
I |
I |
I |
I |
I |
I |
I |
U |
U |
U |
E |
E |
U |
U |
[n]char |
E |
E |
E |
E |
E |
E |
E |
E |
I |
I |
I |
I |
I |
I |
I |
[n]varchar |
E |
E |
E |
E |
E |
E |
E |
E |
I |
I |
I |
I |
I |
I |
I |
datetime |
U |
U |
U |
U |
U |
U |
U |
U |
U |
U |
U |
I |
I |
I |
I |
smalldatetime |
U |
U |
U |
U |
U |
U |
U |
U |
U |
U |
U |
I |
I |
I |
I |
bigdatetime |
U |
U |
U |
U |
U |
U |
U |
U |
U |
U |
U |
I |
I |
I |
I |
bigtime |
U |
U |
U |
U |
U |
U |
U |
U |
U |
U |
U |
I |
I |
U |
I |
tinyint |
I |
I |
I |
I |
I |
I |
I |
I |
U |
U |
U |
E |
E |
U |
U |
smallint |
I |
I |
I |
I |
I |
I |
I |
I |
U |
U |
U |
U |
E |
U |
U |
unsigned smallint |
I |
I |
I |
I |
I |
I |
I |
I |
U |
U |
U |
E |
E |
U |
U |
int |
I |
I |
I |
I |
I |
I |
I |
I |
U |
U |
U |
E |
E |
U |
U |
unsigned int |
I |
I |
I |
I |
I |
I |
I |
I |
U |
U |
U |
E |
E |
U |
U |
bigint |
– |
I |
I |
I |
I |
I |
I |
I |
U |
U |
U |
E |
E |
U |
U |
unsigned bigint |
I |
– |
I |
I |
I |
I |
I |
I |
U |
U |
U |
E |
E |
U |
U |
decimal |
I |
I |
– |
I |
I |
I |
I |
I |
U |
U |
U |
E |
E |
U |
U |
numeric |
I |
I |
I |
– |
I |
I |
I |
I |
U |
U |
U |
E |
E |
U |
U |
float |
I |
I |
I |
I |
– |
I |
I |
I |
U |
U |
U |
E |
E |
U |
U |
real |
I |
I |
I |
I |
I |
– |
I |
I |
U |
U |
U |
E |
E |
U |
U |
money |
I |
I |
I |
I |
I |
I |
– |
I |
U |
U |
U |
E |
E |
U |
U |
smallmoney |
I |
I |
I |
I |
I |
I |
I |
– |
U |
U |
U |
E |
E |
U |
U |
text |
U |
U |
U |
U |
U |
U |
U |
U |
– |
I |
U |
E |
E |
U |
U |
unitext |
U |
U |
U |
U |
U |
U |
U |
U |
I |
– |
I |
U |
U |
U |
U |
image |
U |
U |
U |
U |
U |
U |
U |
U |
U |
I |
– |
E |
E |
U |
U |
unichar |
E |
E |
E |
E |
E |
E |
E |
E |
I |
I |
I |
– |
I |
I |
I |
univarchar |
E |
E |
E |
E |
E |
E |
E |
E |
I |
I |
I |
I |
– |
I |
I |
date |
U |
U |
U |
U |
U |
U |
U |
U |
U |
U |
U |
I |
I |
– |
I |
time |
U |
U |
U |
U |
U |
U |
U |
U |
U |
U |
U |
I |
I |
I |
– |
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.