Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

sql-server

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 ******

I have never seen this with ****'s before, and google searching seems to come up with nothing. Is there a known cause for why SQL Server would provide this message with asterisks?

like image 283
Kyle Avatar asked Jun 28 '18 19:06

Kyle


4 Answers

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.

like image 163
spacebread Avatar answered Oct 24 '22 11:10

spacebread


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.

like image 34
Joshua Kemmerer Avatar answered Oct 24 '22 09:10

Joshua Kemmerer


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.

like image 24
Tom Legg Avatar answered Oct 24 '22 10:10

Tom Legg


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.

like image 33
Razvan Socol Avatar answered Oct 24 '22 11:10

Razvan Socol