0

I have an odd problem in SQL Server. Basically we have a generic type object with two columns, Description 1 and Description 2. We check if Description2 is a number and if it is, check the value/use it. However I am getting the following error when attempting to run the query.

Conversion failed when converting nvarchar value 'Test Other' to data type int.

Now normally I would simply assume that it was attempting to join on improper values/etc, but the varchar it's saying in the error should not be valid from the ISNUMERIC.

Here is a sample of my query:

SELECT *
FROM base_table
JOIN org_type o ON o.id = base_table.org_id
LEFT JOIN generic_object go ON go.id = base_table.org_id AND ISNUMERIC(description2)
WHERE description2 = @orgId
  AND base_table.id = @baseId

What is strange to me, when I remove the last line from the query, I do not receive an error.

5
  • How have you defined @baseId? Tell us both the datatype and value. Commented Aug 31, 2016 at 18:44
  • For the purpose of this, the value is variable but in my system they are currently set to 2535 and 2357 respectively. Commented Aug 31, 2016 at 18:44
  • Do you have any idea where the reported problem value 'Test Other' is coming from? Commented Aug 31, 2016 at 18:45
  • Yes, we have multiple types of "entities". The one I am attempting to JOIN with is an Organization, 'Test Other' is from the "Other Entity" type. I don't know why it is there other then the fact that it is related to the base_table. On that note - "base_table" is a many-to-many table of basically one type of ID to entities. Commented Aug 31, 2016 at 18:51
  • 1
    We eventually realized what the issue was. So simple it was bad. description2 is a varchar. The program we were using to test the scripts was automatically casting it properly, the clients was not. Commented Sep 20, 2016 at 16:46

3 Answers 3

2

Use an inline view to return only numeric description2 records to start with. This way, it has to resolve a data set only containing numeric values first.

SELECT *
FROM base_table
JOIN org_type o ON o.id = base_table.org_id
LEFT JOIN (SELECT * 
           FROM generic_object 
           WHERE isNumeric(description2)) go ON go.id = base_table.org_id
WHERE description2 = @orgId
    AND base_table.id = @baseId
Sign up to request clarification or add additional context in comments.

Comments

2

I haven't ever seen this kind of join... but I think this will get you the desired result. First remove the ISNUMERIC from the join. I basically compare the variable to description2 only if it is numeric, and if it isn't then just compare it to itself which is always true.

WHERE ISNULL(@orgId,'') = case when ISNUMERIC(description2) = 1 then description2 else ISNULL(@orgId,'') end

2 Comments

This seems to work for me, there was some other query issues that this showed me at the same time but the general idea worked perfectly and allowed me to advance. Thanks
No worries at all @Sh4d0wsPlyr
0

To me it looks like the description2 = @orgId is the problem. If you are checking for ISNUMERIC(description2) on the JOIN, the other results you are getting will not be Numeric for that column. So the Where clause is giving you the error. Try moving the ISNUMERIC(description2) = 1 to the Where clause.

1 Comment

Attempted this - same issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.