Menu

SQL Server Error 245: Conversion Failed from varchar to int

Diagnose SQL Server Msg 245 by finding invalid varchar values and implicit conversions, then choose a safe fix with TRY_CONVERT or matching types.

Posted on By
On this page

SQL Server error 245 means the Database Engine could not convert a value to the requested type. A common message is Conversion failed when converting the varchar value 'abc' to data type int. The conversion might be explicit in a CAST or CONVERT, or implicit when an expression combines different data types. See the SQL Server data type precedence order and Microsoft’s data type conversion guidance.

Check for implicit conversion in an expression

When an operator combines different data types, SQL Server converts the lower-precedence type to the higher-precedence type when an implicit conversion is supported. For example, int has higher precedence than varchar, so this expression tries to convert the text to an integer before evaluating +:

DECLARE @number int = 1;

SELECT @number + ' is not a number';

This raises Msg 245 because the text cannot become an int. The + operator would concatenate strings only if both operands were strings. Check the full statement for mixed-type comparisons, joins, CASE expressions, UNION branches, or arithmetic expressions that rely on an unexpected implicit conversion. Microsoft’s data type precedence rules describe which side SQL Server converts.

Find text values that cannot convert to int

If a source column contains text that should represent integers, use TRY_CONVERT to locate values that fail conversion before changing the query or column:

SELECT ImportID, RawValue
FROM dbo.ImportRows
WHERE RawValue IS NOT NULL
  AND TRY_CONVERT(int, RawValue) IS NULL;

TRY_CONVERT returns NULL when a supported conversion fails; the RawValue IS NOT NULL condition keeps existing NULL values separate from invalid text. Do not silently discard or replace the returned rows until the application has a defined rule for them. See the SQL Server TRY_CONVERT() reference and Microsoft’s function documentation.

If the values are numeric but stored as text, clean and validate them before converting the column or loading them into an integer column. If they are identifiers that may contain leading zeros or nonnumeric characters, keep them as character data and compare them with the matching type instead of forcing an integer conversion. In application queries, also bind parameters using the type that matches the target column.

Use CAST or CONVERT() when the source value is known to be valid and an explicit type change is intended. An explicit conversion does not make invalid input valid; verify the input format and range first. For SQL Server integer ranges, see the int reference.

Browse the SQL Server error troubleshooting index for related message guides.