T-SQL TRY_CONVERT() Function
The T-SQL (Transact-SQL) TRY_CONVERT() function tries to convert an expression from one datatype to another datatype. If the conversion fails, the function will return NULL. Otherwise, it will return the converted value.
Syntax
TRY_CONVERT( data_type [ ( length ) ] , expression [ , style ] )
Parameters
data_type |
Required. Specify the datatype to convert to. See the data types of T-SQL. 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 TRY_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 TRY_CONVERT(INT, 123.456); Result: 123
Convert to DECIMAL
The TRY_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 TRY_CONVERT(DECIMAL(5, 2), '123.456'); Result: 123.46
Convert to CHAR
The TRY_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 TRY_CONVERT(CHAR(4), 123); Result: '123'
Convert to NCHAR
The TRY_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 TRY_CONVERT(NCHAR(10), 123); Result: '123'
Convert to VARCHAR
The TRY_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 TRY_CONVERT(VARCHAR, 123); Result: '123'
Convert to DATE
The TRY_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 TRY_CONVERT(DATE, '2018-08-18'); Result: '2018-08-31'
Convert to TIME
The TRY_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 TRY_CONVERT(TIME, '10:38:42'); Result: '10:38:42'
Convert to DATETIME
The TRY_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 TRY_CONVERT(DATETIME, '2018-08-18 10:38:42', 101); Result: '2018-08-18 10:38:42'
❮ T-SQL Functions