I have a data column that contains 9 digits of numeric values, but the column is defined as varchar. I need a CAST field for a numerical value, so I can divide the sum of the column by 100. For example,
select CAST(field1 as numeric(9,2)) / 100 from table;
The following error occurs when starting the query: Arithmetic overflow error converting varchar to numeric data type.
If I do double CAST from varchar -> int -> numeric, CAST works. eg.
select CAST(CAST(field1 as int) as numeric(9,2)) / 100 from table;
Is there a reason why a single CAST from varchar -> numeric results in an SQL error but double CAST works?
source share