I came across a bug where I was using CAST(Col1 AS INT) + CAST(Col2 AS INT)
where both Col1 and Col2 are VARCHAR
and I was getting valid results out when Col1 or Col2 was blank and I didn't expect this. I checked and CAST (and CONVERT) both have this default behavior of replacing blank with 0:
SELECT CAST('' AS INT)
SELECT CONVERT(INT, '')
I checked the info page and I can't see any reference to explain why this is the behavior (or change it through a server setting). I can of course work around this but I wanted to ask why this is the behavior as I do not think it is intuitive.
I'd actually rather this CAST
failed or gave NULL
, is there a server setting somewhere which effects this?