I am querying a database in which I am getting the following exception:
Conversion failed when converting varchar value 'B6UJ978023EC' to data type int
How can I fix it?
I am querying a database in which I am getting the following exception:
Conversion failed when converting varchar value 'B6UJ978023EC' to data type int
How can I fix it?
My guess is that your query looks something like this:
select * from mytable where SomeField = 'B6UJ978023EC'
If that's the case, SomeField
was declared as an INT
datatype and Sql Server is attempting to convert 'B6UJ978023EC'
to an INT
which won't work.
It's possible that you're trying to do an insert/update, so this wouldn't work either if SomeField is an INT.
update mytable
set SomeField = 'B6UJ978023EC'
where something = something
insert mytable (field1, field2, SomeField)
values(123, 'abc', 'B6UJ978023EC')
Same result, Conversion failed when converting varchar value 'B6UJ978023EC' to data type int
In this case, either change the datatype of the column, or change the value of the variable.
Update:
If your column's datatype is (N)VARCHAR
but contains some real numbers, then you can simply add single quotes around the number you're looking for.
-- works
select * from mytable where SomeField = '123456'
-- doesn't work - same error
select * from mytable where SomeField = 123456
try this:
Error is because you are trying to convert a character value to integer value. Just add the below condition to the query so that it will select only integer values
where ISNUMERIC(col+ '.0e0')=1