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

Published: 29 September 2021
on channel: SQL Server 101
9,346
65

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:
70-461, 70-761 Querying Microsoft SQL Server with T-SQL: https://rebrand.ly/querying-microsoft...
98-364: Database Fundamentals (Microsoft SQL Server): https://rebrand.ly/database-fundamentals
70-462 SQL Server Database Administration (DBA): https://rebrand.ly/sql-server-dba
Microsoft SQL Server Reporting Services (SSRS): https://rebrand.ly/sql-server-ssrs
SQL Server Integration Services (SSIS): https://rebrand.ly/sql-server-ssis
SQL Server Analysis Services (SSAS): https://rebrand.ly/sql-server-ssas-mdx
Microsoft Power Pivot (Excel) and SSAS (Tabular DAX model): https://rebrand.ly/microsoft-powerpiv...
----
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.


Watch video Convert numbers and dates into strings in SQL Server using the CAST and TRY_CAST functions online without registration, duration hours minute second in high quality. This video was added by user SQL Server 101 29 September 2021, don't forget to share it with your friends and acquaintances, it has been viewed on our site 9,346 once and liked it 65 people.