create table #t(col varchar(10))
insert into #t values(1042800)
select col, ROUND(col,2) as RoundVal from #t
update #t set col = ROUND(col,2)
The select col, ROUND(col,2) from #t returns - as expected:
| col | RoundVal |
|---|---|
| 1042800 | 1042800 |
But update #t set col = ROUND(col,2) throws the following error:
Arithmetic overflow error for type varchar, value = 1042800.000000.
Question: I understand the error: I'm trying to convert a number into a VARCHAR variable, and the specified length of the VARCHAR variable is too short to accommodate the value. But why the update statement is adding 6 trailing zeros whereas select statement is (as expected) not adding any trailing zeros.
select SQL_Variant_Property( Round( '1042800', 2 ), 'basetype' );returnsfloat.select Cast( Round( '1042800', 2 ) as VarChar(32) );returns1.0428e+006. Some study of data type precedence is useful before using data types in curious ways, e.g. mathematical operations on strings. dbfiddle.I'm trying to convert a number into a VARCHAR variable- Why do you want to do that? Sounds like X & Y problem, it's usually not needed