user3262364
user3262364

Reputation: 371

Operand data type varchar is invalid for subtract operator. in SQL Server

I have a query like this :

Update T_Assets set F_Asset_CurrentValue=(F_Asset_CurrentValue - '99.58')
Where F_Asset_Code='ITSRDL00001'

but I'm getting an error:

Msg 8117, Level 16, State 1, Line 1
Operand data type varchar is invalid for subtract operator

Upvotes: 1

Views: 7664

Answers (1)

David דודו Markovitz
David דודו Markovitz

Reputation: 44961

You are getting this error because both sides of of the arithmetic operation are of character type.

In the following example there is an implicit conversion of '123' to 123

select      456   - '123';

In the following example there is an implicit conversion of '456' to 123

select      '456' - 123;

The following example yields in a error

select      '456' - '123';

Msg 402, Level 16, State 1, Line 1
The data types varchar and varchar are incompatible in the subtract operator.

My recommendation is to use explicit conversion

cast (F_Asset_CurrentValue as decimal (32,2)) - 99.58

Upvotes: 3

Related Questions