Converting between datatypes

Adaptive Server automatically handles many conversions from one datatype to another. These are called implicit conversions. You can explicitly request other conversions with the convert, inttohex, and hextoint functions. Other 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, if they can be interpreted as datetime values. However, for the purposes of display, you must use the convert function to convert char to int. Similarly, you must use convert on integer data if you want 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, to display all sales beginning with the digit 2:

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

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

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