convert

Description

Returns the specified value, converted to another datatype or a different datetime display format.

Syntax

convert (datatype [(length) | (precision[, scale])] 
	[null | not null], expression [, style])

Parameters

datatype

is the system-supplied datatype (for example, char(10), unichar (10), varbinary (50), or int) into which to convert the expression. You cannot use user-defined datatypes.

When Java is enabled in the database, datatype can also be a Java-SQL class in the current database.

length

is an optional parameter used with char, nchar, unichar, univarchar, varchar, nvarchar, binary and varbinary datatypes. If you do not supply a length, Adaptive Server truncates the data to 30 characters for the character types and 30 bytes for the binary types. The maximum allowable length for character and binary expression is 64K.

precision

is the number of significant digits in a numeric or decimal datatype. For float datatypes, precision is the number of significant binary digits in the mantissa. If you do not supply a precision, Adaptive Server uses the default precision of 18 for numeric and decimal datatypes.

scale

is the number of digits to the right of the decimal point in a numeric, or decimal datatype. If you do not supply a scale, Adaptive Server uses the default scale of 0.

null | not null

specifies the nullabilty of the result expression. If you do not supply either null or not null, the converted result has the same nullability as the expression.

expression

is the value to be converted from one datatype or date format to another.

When Java is enabled in the database, expression can be a value to be converted to a Java-SQL class.

When Unichar is used as the destination data type, the default length of 30 Unicode values is used if no length is specified.

style

is the display format to use for the converted data. When converting money or smallmoney data to a character type, use a style of 1 to display a comma after every 3 digits.

When converting datetime or smalldatetime data to a character type, use the style numbers in Table 2-4 to specify the display format. Values in the left-most column display 2-digit years (yy). For 4-digit years (yyyy), add 100, or use the value in the middle column.

Table 2-4: Display formats for date/time information

Without century (yy)

With century (yyyy)

Output

N/A

0 or 100

mon dd yyyy hh:miAM (or PM)

1

101

mm/dd/yy

2

102

yy.mm.dd

3

103

dd/mm/yy

4

104

yy.mm.dd

5

105

dd-mm-yy

6

106

dd mon yy

7

107

mon dd, yy

8

108

hh:mm:ss

N/A

9 or 109

mon dd yyyy hh:mi:ss:mmmAM (or PM)

10

110

dd-mm-yy

11

111

yy/mm/dd

12

112

yymmdd

The default values (style 0 or 100), and style 9 or 109 return the century (yyyy). When converting to char or varchar from smalldatetime, styles that include seconds or milliseconds show zeros in those positions.

Examples

Example 1

select title, convert(char(12), total_sales) 
from titles

Example 2

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

Example 3

Converts the current date to style “3”, dd/mm/yy:

select convert(char(12), getdate(), 3)

Example 4

If the value pubdate can be null, you must use varchar rather than char, or errors may result:

select convert(varchar(12), pubdate, 3) from titles 

Example 5

Returns the integer equivalent of the string “0x00000100”. Results can vary from one platform to another:

select convert(integer, 0x00000100)

Example 6

Returns the platform-specific bit pattern as a Sybase binary type:

select convert (binary, 10)

Example 7

Returns 1, the bit string equivalent of $1.11:

select convert(bit, $1.11)

Example 8

Creates #tempsales with total_sales of datatype char(100), and does not allow null values. Even if titles.total_sales was defined as allowing nulls, #tempsales is created with #tempsales.total_sales not allowing null values:

select title, convert (char(100) not null, total_sales) into #tempsales 
from titles

Usage


Conversions involving Java classes

Standards

SQL92 – Compliance level: Transact-SQL extension.

Permissions

Any user can execute convert.

See also

Datatypes User-defined datatypes

Functions hextoint, inttohex