Skip to main content

Conversion functions

Overview

These functions support data type casting and conversion. Conversion functions convert an expression of one data type to another data type. The conversion functions covered in this section are:

Both 'CAST' and 'CONVERT' functions perform the same basic operation of data type conversion in SQL. The key difference lies in their flexibility. 'CONVERT' has an additional style parameter, which makes it more flexible in formatting output data.

CAST

This function is used to translate an expression of one data type to another.

Syntax

CAST ( expression AS data_type [ ( length ) ] )

Return Type

Returns expression, translated to data_type

Arguments

ArgumentDescriptionExample
expressionAn expression to translate.1.5
data_typeThe data type to translate the expression into.int
lengthAn optional integer that specifies the length of the target data type, for data types that allow a user specified length.

Example

The below example translates the expression 1.5 as an integer value.

SELECT CAST(1.5 AS int)

Image 1: Cast Function Results

CONVERT

This function is used to convert an expression of one data type to another.

Syntax

CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

Return Type

Returns expression, converted to data_type

Arguments

ArgumentDescriptionExample
data_typeThe data type to convert the expression into.datetime
lengthAn optional integer that specifies the length of the target data type, for data types that allow a user specified length.
expressionAn expression to convert.2020-01-01
styleAn optional integer expression that specifies how the CONVERT function will translate expression. For a style value of NULL, NULL is returned.103

Converting datetime to character

Without centuryWith centuryInput/OutputStandard
0100mon dd yyyy hh:miAM/PMDefault
1101mm/dd/yyyyUS
2102yyyy.mm.ddANSI
3103dd/mm/yyyyBritish/French
4104dd.mm.yyyyGerman
5105dd-mm-yyyyItalian
6106dd mon yyyy-
7107Mon dd, yyyy-
8108hh:mm:ss-
9109mon dd yyyy hh:mi:ss:mmmAM (or PM)Default + millisec
10110mm-dd-yyyyUSA
11111yyyy/mm/ddJapan
12112yyyymmddISO
13113dd mon yyyy hh:mi:ss:mmmEurope (24 hour clock)
14114hh:mi:ss:mmm24 hour clock
20120yyyy-mm-dd hh:mi:ssODBC canonical (24 hour clock)
21121yyyy-mm-dd hh:mi:ss.mmmODBC canonical (24 hour clock)
-126yyyy-mm-ddThh:mi:ss.mmmISO8601
-127yyyy-mm-ddThh:mi:ss.mmmZISO8061 (with time zone Z)
-130dd mon yyyy hh:mi:ss:mmmAMHijiri
-131dd/mm/yy hh:mi:ss:mmmAMHijiri

Converting float to real

ValueExplanation
0Maximum 6 digits (default)
18 digits
216 digits

Converting money to character

ValueExplanation
0No comma delimiters, 2 digits to the right of decimal
1Comma delimiters, 2 digits to the right of decimal
2No comma delimiters, 4 digits to the right of decimal

Example 1

The below example converts '2020-01-01' to datetime.

SELECT CONVERT(datetime, '2020-01-01')

Example 2

The below example converts a text valued column (Date of Purchase) into a date valued column (Event Date).

103 refers to the style code.

Syntax

SELECT [Text Date Column],
CONVERT(DATE, [Your Text Date Column], 103) AS `Converted Date Column`
FROM [Domain].[Table]
WHERE [Deleted] IS NULL

Example

SELECT [Date of Purchase],
CONVERT(DATE, [Date of Purchase], 103) AS `EventDate`
FROM [Your Domain].[Your Table]
WHERE [Deleted] IS NULL

Conversion Example