Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output of adding an integer and a String in SQL Server

Tags:

sql

sql-server

I was asked what the following simple query would return.

Select 5 + '4'

As, 4 is string type, I expect the query to throw an error. I found I was wrong, tested it, and the result was 9. How does it return the sum?

like image 533
BlackCat Avatar asked Aug 12 '16 11:08

BlackCat


1 Answers

If you mix data types such as INT and VARCHAR, SQL Server will always attempt to convert everything to the one that has the highest precedence.

like image 63
Roman Marusyk Avatar answered Sep 19 '22 20:09

Roman Marusyk