Hugo Kornelis (3/25/2010)
Good question, but unfortunately the explanation is not entirely correct. Especially this part is very misleading:Also the preceding space is not considered while performing the operation.
. . . .
Order of precedence dictates conversion of varchar to int, and ' ' is converted to the value 0
. . . .
Thanks for the detailed explanation, Hugo. You beat me to it in pointing out that spaces (or an empty string) cast to numeric as value zero. A slight twist on the QOD code would dramatically illustrate that the leading spaces are not at all ignored:
declare @a int
declare @b-2 varchar(2)
set @a = 10
set @b-2 = ' '+@a + 2
set @b-2 = ' '*@a + 2 --Multiply instead
Results are:
Original
-----------
22
(1 row(s) affected)
Revised
-----------
12
(1 row(s) affected)