Convert numbers and dates into strings in SQL Server using the CAST and TRY_CAST functions

preview_player
Показать описание
SELECT 3 & 'H' doesn't work in SQL Server. In this video, we'll look at how you can convert the number 3 into a string.
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 CAST function.
However, what happens if the CAST function is unsuccessful? You get another error message. However, if you want to suppress the error message, then you can use the TRY_CAST function instead. If it is now unsuccessful, you get a NULL instead of an error message.
Рекомендации по теме
Комментарии
Автор

Thank you very much for the clear explanation. This channel will grow for sure!

mosespitso
Автор

How do I do it if I want to retrieve the current date and time using getdate, then enter it into a column of numeric data type?

alifpurnama