Conversion failed when converting the ****** value '******' to data type ******

前端 未结 4 1738
春和景丽
春和景丽 2021-02-20 05:18

I\'m getting this error from SQL Server on SSMS 17 while running a giant query:

Conversion failed when converting the ****** value \'******\' to data type

4条回答
  •  我在风中等你
    2021-02-20 05:48

    My error was also: Conversion failed when converting the ****** value '******' to data type ******.

    My issue ended up being in my join, which was joining a varchar(50) field on an int field.

    SELECT TOP (10) Product.Name, ProductDetails.Price
    FROM Product
    --the ProductDetails.Product field is a varchar field that contains 
    --a string of the ProductID which is an int in the Product table
    FULL JOIN Product.ProductID = ProductDetails.Product 
    

    I corrected this by casting the int field to a varchar

    FULL JOIN CAST(Product.ProductID as varchar(50)) = ProductDetails.Product
    

    P.S. I went back and later changed to a Left outer join which made more since for our business logic. Also I did not test this code, it is modified for illustration purposes.

    Details on reproducing: Sometimes the query would work fine. I found I personally had issues when I had TWO string comparisons in my WHERE clause e.g.

    WHERE field like '%' + @var + '%'
    OR field2 like '%' + @var2 + '%'
    

    If I had just one comparison, it seemed to work fine.

提交回复
热议问题