SQL Server CONVERT() Function
The SQL Server (Transact-SQL) 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.
Syntax
CONVERT( data_type [ ( length ) ] , expression [ , style ] )
Parameters
data_type |
Required. Specify the datatype to convert to. See the data types of SQL Server. Note that the Alias data types cannot be used. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
length |
Optional. Specify the length of the resulting data type, for data types that allow a user specified length. Default is 30. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
expression |
Required. Specify the value to convert to another datatype. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
style |
Date and Time stylesFor a date or time data type expression, style can can be one of the following values:
float and real stylesFor a float or real data type expression, style can can be one of the following values:
money and smallmoney stylesFor a money and smallmoney data type expression, style can can be one of the following values:
|
Return Value
Returns the expression converted to another data_type.
Convert to INT
The CONVERT() function can be used to convert a value to a INT type. For example - in the example below, 123.456 is converted to INT datatype.
SELECT CONVERT(INT, 123.456); Result: 123
Convert to DECIMAL
The CONVERT() function can be used to convert a value to a DECIMAL type. For example - in the example below, '123.456' is converted to DECIMAL datatype.
SELECT CONVERT(DECIMAL(5, 2), '123.456'); Result: 123.46
Convert to CHAR
The CONVERT() function can be used to convert a value to a CHAR type. For example - in the example below, 123 is converted to CHAR datatype.
SELECT CONVERT(CHAR(4), 123); Result: '123'
Convert to NCHAR
The CONVERT() function can be used to convert a value to a NCHAR type. For example - in the example below, 123 is converted to NCHAR datatype.
SELECT CONVERT(NCHAR(10), 123); Result: '123'
Convert to VARCHAR
The CONVERT() function can be used to convert a value to a VARCHAR type. For example - in the example below, 123 is converted to VARCHAR datatype.
SELECT CONVERT(VARCHAR, 123); Result: '123'
Convert to DATE
The CONVERT() function can be used to convert a value to a DATE type. For example - in the example below, '2018-08-18' is converted to DATE datatype.
SELECT CONVERT(DATE, '2018-08-18'); Result: '2018-08-31'
Convert to TIME
The CONVERT() function can be used to convert a value to a TIME type. For example - in the example below, '10:38:42' is converted to TIME datatype.
SELECT CONVERT(TIME, '10:38:42'); Result: '10:38:42'
Convert to DATETIME
The CONVERT() function can be used to convert a value to a DATETIME type. For example - in the example below, '2018-08-18 10:38:42' is converted to DATETIME datatype.
SELECT CONVERT(DATETIME, '2018-08-18 10:38:42', 101); Result: '2018-08-18 10:38:42'
❮ SQL Server Functions