Редактиране

Споделяне чрез


TRY_CONVERT (Transact-SQL)

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

Syntax

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

Arguments

data_type

The data type into which to cast expression.

length

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.

expression

The value to cast.

style

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.

Return types

Returns a value cast to the specified data type if the cast succeeds; otherwise, returns NULL.

Remarks

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).

Examples

A. TRY_CONVERT returns NULL

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

B. TRY_CONVERT fails with an error

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.

C. TRY_CONVERT succeeds

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