Reputation: 69
I am having problems with a query.
SELECT table_1.element_description, table_1.element_id, ISNULL(exercise_time_taken, '00:00:00') AS exercise_time_taken
FROM (SELECT * FROM elements WHERE **`unit_id = 'ndb'`**) as table_1
LEFT JOIN(SELECT CONVERT(CHAR(8), DATEADD(SS, SUM(DATEDIFF(ss, start_time, finish_time)),'20000101'), 108) AS exercise_time_taken, element_id
FROM exercises
WHERE exercises.user_id = 'my_email' **AND element_id <> ALL (SELECT unit_id FROM units)**
GROUP BY element_id) as table_2 ON table_1.element_id = table_2.element_id
When i replace the highlighted line AND element_id <> ALL (SELECT unit_id FROM units)
WITH
element_id <> 'admin' AND element_id <> 'fc1' AND element_id <> 'ncc1' AND element_id <> 'ncc2' AND element_id <> 'nda' AND element_id <> 'ndb' AND element_id <> 'ngc1' AND element_id <> 'ngc1_demo' AND element_id <> 'ngc2' AND element_id <> 'ngc3'
Which is basically the long way of specifying which values not to select, the query runs fine a produced the desired output, if i dont do it this way then i get the 'Conversion failed when converting the nvarchar value 'ngc1' to data type int' error.
I would prefer not to manually code for each value as the list will grow over time and I will have to keep updating the query, which inevitably will be missed at some point causing more work than necessary.
Why does it do this? Even stanger depending on which unit i initially specify see sample line, it works for some and not others.
Upvotes: 1
Views: 9039
Reputation: 2800
What is the data type of unit_id in the units table? If this is an int then that is why you are getting this error. It is trying to convert element_id to compare against your subquery values and the conversion fails.
Try using the following code and see if it works:
AND element_id <> ALL (SELECT CONVERT(VARCHAR, unit_id) FROM units)
Upvotes: 1