Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies to:
SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
Analytics Platform System (PDW)
SQL analytics endpoint in Microsoft Fabric
Warehouse in Microsoft Fabric
Returns a value cast to the specified data type if the cast succeeds; otherwise, returns NULL
.
Transact-SQL syntax conventions
TRY_CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
The data type into which to cast expression.
An optional integer that specifies the length of the target data type, for data types that allow a user specified length. The maximum value for length is 8,000 bytes.
The value to cast.
Optional integer expression that specifies how the TRY_CONVERT
function is to translate expression.
style accepts the same values as the style parameter of the CONVERT
function. For more information, see CAST and CONVERT.
The value of data_type determines the range of acceptable values. If style is NULL
, then TRY_CONVERT
returns NULL
.
Returns a value cast to the specified data type if the cast succeeds; otherwise, returns NULL
.
TRY_CONVERT
takes the value passed to it and tries to convert it to the specified data_type. If the cast succeeds, TRY_CONVERT
returns the value as the specified data_type; if an error occurs, NULL
is returned. However if you request a conversion that is explicitly not permitted, then TRY_CONVERT
fails with an error.
TRY_CONVERT
is a reserved keyword, starting with compatibility level 110
.
This function is capable of being remoted to servers that have SQL Server 2012 (11.x) and later versions. It isn't remoted to servers that have a version earlier than SQL Server 2012 (11.x).
The following example demonstrates that TRY_CONVERT
returns NULL
when the cast fails.
SELECT
CASE WHEN TRY_CONVERT(FLOAT, 'test') IS NULL
THEN 'Cast failed'
ELSE 'Cast succeeded'
END AS Result;
GO
Here's the result set.
Result
------------
Cast failed
The following example demonstrates that the expression must be in the expected format.
SET DATEFORMAT dmy;
SELECT TRY_CONVERT(DATETIME2, '12/31/2022') AS Result;
GO
Here's the result set.
Result
----------------------
NULL
The following example demonstrates that TRY_CONVERT
returns an error when the cast is explicitly not permitted.
SELECT TRY_CONVERT(XML, 4) AS Result;
GO
The result of this statement is an error, because an integer can't be cast into an xml data type.
Explicit conversion from data type int to xml is not allowed.
This example demonstrates that the expression must be in the expected format.
SET DATEFORMAT mdy;
SELECT TRY_CONVERT(DATETIME2, '12/31/2022') AS Result;
GO
Here's the result set.
Result
----------------------------------
2022-12-31 00:00:00.0000000
Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register today