SQL Server: CONVERT Function

CONVERT function 

This SQL Server tutorial explains how to use the CONVERT function in SQL Server (Transact-SQL) with syntax and examples.

Description

In SQL Server (Transact-SQL), the CONVERT function converts an expression from one datatype to another datatype. If the conversion fails, the function will return an error. Otherwise, it will return the converted value.

TIP: Use the TRY_CONVERT function to return a NULL (instead of an error) if the conversion fails.

Syntax

The syntax for the CONVERT function in SQL Server (Transact-SQL) is:

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

Parameters or Arguments

type

The datatype that you wish to convert expression to. It can be one of the following: bigint, int, smallint, tinyint, bit, decimal, numeric, money, smallmoney, float, real, datetime, smalldatetime, char, varchar, text, nchar, nvarchar, ntext, binary, varbinary, or image.

length

Optional. The length of the resulting data type for char, varchar, nchar, nvarchar, binary and varbinary.

expression

The value to convert to another datatype.

style

Optional. The format used to convert between datatypes, such as a date format or string format. It can be one of the following values:

Converting datetime to character

Value (without century)Value (with century)Explanation
0100mon dd yyyy hh:miAM/PM (Default)
1101mm/dd/yyyy (US standard)
2102yy.mm.dd (ANSI standard)
3103dd/mm/yy (British/French standard)
4104dd.mm.yy (German standard)
5105dd-mm-yy (Italian standard)
6106dd mon yy
7107Mon dd, yy
8108hh:mi:ss
9109mon dd yyyy hh:mi:ss:mmmAM/PM
10110mm-dd-yy (USA standard)
11111yy/mm/dd (Japan standard)
12112yymmdd (ISO standard)
13113dd mon yyyy hh:mi:ss:mmm (Europe standard – 24 hour clock)
14114hh:mi:ss:mmm (24 hour clock)
20120yyyy-mm-dd hh:mi:ss (ODBC canonical – 24 hour clock)
21121yyyy-mm-dd hh:mi:ss:mmm (ODBC canonical – 24 hour clock)
 126yyyy-mm-ddThh:mi:ss:mmm (ISO8601 standard)
 127yyyy-mm-ddThh:mi:ss:mmmZ (ISO8601 standard)
 130dd mon yyyy hh:mi:ss:mmmAM/PM (Hijri standard)
 131dd/mm/yy hh:mi:ss:mmmAM/PM (Hijri standard)

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 (ie: 1234.56)
1Comma delimiters, 2 digits to the right of decimal (ie: 1,234.56)
2No comma delimiters, 4 digits to the right of decimal (ie: 1234.5678)

Note

  • When casting from a float or numeric to an integer, the CONVERT function will truncate the result. For other conversions, the CONVERT function will round the result.
  • See also the TRY_CONVERT, CAST, and TRY_CAST functions.

Applies To

The CONVERT function can be used in the following versions of SQL Server (Transact-SQL):

  • SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

Example

Let’s look at some SQL Server CONVERT function examples and explore how to use the CONVERT function in SQL Server (Transact-SQL).

For example:

SELECT CONVERT(int, 14.85);
Result: 14          (result is truncated)

SELECT CONVERT(float, 14.85);
Result: 14.85       (result is not truncated)

SELECT CONVERT(varchar, 15.6);
Result: '15.6'

SELECT CONVERT(varchar(4), 15.6);
Result: '15.6'

SELECT CONVERT(float, '15.6');
Result: 15.6

SELECT CONVERT(datetime, '2014-05-02');
Result: '2014-05-02 00:00:00.000'

SELECT CONVERT(varchar, '05/02/2014', 101);
Result: '05/02/2014'

This Post Has One Comment

Leave a Reply