Converting between datatypes

Adaptive Server automatically handles many conversions from one datatype to another. These are called implicit conversions. Use the convert, inttohex, and hextoint functions to explicitly request other conversions. Some conversions cannot be performed, either explicitly or automatically, because of incompatibilities between the datatypes.

For example, Adaptive Server automatically converts char expressions to datetime for the purposes of a comparison, both expressions can be interpreted as datetime values. However, for display purposes, you must use the convert function to convert char to int. Similarly, you must use convert on integer data for Adaptive Server to treat it as character data so that you can use the like keyword with it.

The syntax for the convert function is:

convert (datatype, expression, [style]) 

In the following example, convert displays the total_sales column using the char datatype, showing all sales beginning with the digit 2:

select title, total_sales 
from titles 
where convert (char(20), total_sales) like "2%" 

Use the optional style parameter to convert datetime values to char or varchar datatypes to get a wide variety of date display formats.

See Chapter 16, “Using Transact-SQL Functions in Queries” for details on the convert, inttohex, and hextoint functions.