Question

I have a big query throwing an error that I've whittled down to fairly small chunk.

SELECT
    Reconciliation_Receiving.UserId, *
FROM
    Reconciliation_Receiving 
    LEFT JOIN Profiles.dbo.UserObject ReceivingUser ON Reconciliation_Receiving.UserId = ReceivingUser.g_user_id

The error that is thrown is "Conversion failed when converting from a character string to uniqueidentifier."

The error seems to be connected with the LEFT JOIN, between UserObject and Reconciliation_Receiving, since if I remove it, the query executes.

Reconciliation_Receiving.UserId is a UNIQUEIDENTIFIER while the ReceivingUser.g_user_id is GUID(nchar(38)). This difference could be relevant, I assume, but I'm not seeing how exactly. As far as I know, both have valid guids as data (though this could be wrong, and is one avenue I'm, going to keep looking at.) The particular case I'm looking at has a WHERE clause (omitted for brevity) where all of the Reconciliation_Receiving.UserId values are NULL...but I guess the bad data could be elsewhere, since it's my understanding that the join is done before the WHERE filtering is done.

I tried searching the length of the data stored in the UserObject column to see if some bad data had somehow gotten stored there, but nothing jumped out.

That said, what could be wrong with the data? How to find it? Or could it be something else?

NEW INFO : When I try the following:

SELECT CAST(g_user_id AS uniqueidentifier) 
FROM Profiles.dbo.UserObject

It starts printing some values and then bam, same error. So I'm definitely thinking bad data. But still no idea yet how to actually find it.

Was it helpful?

Solution

The message tells you the exact problem: A string could not be converted to a guid. Now you know: a) there is a conversion somewhere; b) the string data is inconvertible.

"As far as I know, both have valid guids as data"

No, the message states that this is not the case. The message is very clear on this.

Find the invalid data. For example, like this:

SELECT *
FROM ReceivingUser
WHERE TRY_CONVERT(UNIQUEIDENTIFIER, ReceivingUser.g_user_id) IS NULL AND ReceivingUser.g_user_id IS NOT NULL

You better store Guids not as strings but as UNIQUEIDENTIFIER so that you don't have trouble like this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top