Search

Wednesday, April 18, 2012

Error converting data type varchar to int

When inserting or updating data into a table then you may come across this error "Error converting data type varchar to int". As the name indicates this error is comming because you have a field which is integer and while querying he passes any data which is not convertible to int 


Solutions


The first and best solution is to make sure that the data passing to sql server is compatible with datatype. i.e. if passing a string to an int field it should be convertible to integer say '0','222' etc


And if trying to copy data from one table to another there is chance to fire this error if either the column mapping is not correct or there is null in the source field


If there is null in the source field use isnull function. See the following query


INSERT INTO Table1(id) SELECT ISNULL(id, 0) FROM Table2

No comments:

Post a Comment