filmov
tv
Convert numbers and dates into strings in SQL Server using the CONVERT and TRY_CONVERT functions

Показать описание
In this video, we'll look at the CONVERT and TRY_CONVERT functions, and the advantages to using them compared with the CAST and TRY_CAST functions.
My SQL Server Udemy courses are:
----
SELECT 3 & 'H' gives the following error message:
Conversion failed when converting the varchar value 'H' to data type int.
It's because, instead of trying to convert 3 to a string, SQL Server tries to convert 'H' to a number.
In this video, we'll look at how to convert the number 3 to a string, so you can then get the answer '3H', using the CONVERT function.
However, what happens if the CONVERT function is unsuccessful? You get another error message. However, if you want to suppress the error message, then you can use the TRY_CONVERT function instead. If it is now unsuccessful, you get a NULL instead of an error message.
However, why use the CONVERT function when you could use the CAST function instead? There's an advantage to using the CONVERT function if you are CONVERTing dates to strings, as you can choose the style of the conversion.
My SQL Server Udemy courses are:
----
SELECT 3 & 'H' gives the following error message:
Conversion failed when converting the varchar value 'H' to data type int.
It's because, instead of trying to convert 3 to a string, SQL Server tries to convert 'H' to a number.
In this video, we'll look at how to convert the number 3 to a string, so you can then get the answer '3H', using the CONVERT function.
However, what happens if the CONVERT function is unsuccessful? You get another error message. However, if you want to suppress the error message, then you can use the TRY_CONVERT function instead. If it is now unsuccessful, you get a NULL instead of an error message.
However, why use the CONVERT function when you could use the CAST function instead? There's an advantage to using the CONVERT function if you are CONVERTing dates to strings, as you can choose the style of the conversion.