Saturday , December 21 2024

TRY_CAST and TRY_CONVERT Functions in SQL Server(TSQL)

 

We will examine TRY_CAST and TRY_CONVERT functions in this article. You can find more informartion about CAST and CONVERT functions in the article below.

CAST and CONVERT Functions in SQL Server(TSQL)

The TRY_CAST function executes the CAST function. If the CAST function is completed successfully, it returns the result, otherwise it returns NULL. If it is not completed successfully, we can use “case when” to return different values than null when it fails.

The TRY_CONVERT function executes the CONVERT function. If the CONVERT function is completed successfully, it returns the result, otherwise it returns NULL. If it is not completed successfully, we can use “case when” to return different values than null when it fails.

Let’s make examples for a better understanding of these functions.

Examples:

TRY_CAST with successful conversion:

TRY_CAST with failed conversion:

TRY_CAST with CASE WHEN Statement:

TRY_CONVERT with successful conversion:

TRY_CONVERT with failed conversion:

TRY_CONVERT with CASE WHEN Statement:

Loading

About dbtut

We are a team with over 10 years of database management and BI experience. Our Expertises: Oracle, SQL Server, PostgreSQL, MySQL, MongoDB, Elasticsearch, Kibana, Grafana.

Leave a Reply

Your email address will not be published. Required fields are marked *