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

前端 未结 4 1775
春和景丽
春和景丽 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:38

    Are you getting data from a view? If so, you might be having issues with the view you're using. For example, in my case, I was pulling from a view and when I tried to view it in SSMS it showed me more details about what the error was.

    0 讨论(0)
  • 2021-02-20 05:39

    I had the same error when trying to combine an nvarchar and int into a single string. Usually it tells me the data type where I screwed up but occasionally its the error you got.

    I went from

    SELECT ';' + [myNumber]
    

    to

    SELECT ';' + CAST([myNumber] as nvarchar(100))
    

    which solved it for me.

    So as the others have suggested, I guess it is something similar and you need to CAST or CONVERT one of your values to match the rest.

    0 讨论(0)
  • 2021-02-20 05:46

    The asterisks appear when you do not have enough permissions. It seems they appear only in some scenarios (in my case, when using user-defined functions).

    I don't know why, but it seems that if you GRANT UNMASK, you will see the real values.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题