I have a query as following:
select q.question, e.filename
from question q
join actions a on a.FK_QId = q.PK_Id
join action_params ap on ap.FK_AId = a.PK_Id
join envfile e on e.FK_Qid = ap.value
where e.EnvType = 0
The problem is the JOIN ON clause e.FK_Qid = ap.value where value is varchar and FK_QId is integer. I know I have to do something with CAST and CONVERT but what I tried failed:
select q.`question`, e.filename
from question q
join actions a on a.FK_QId = q.PK_Id
join action_params ap on ap.FK_AId = a.PK_Id
join envfile e on CAST(e.FK_Qid as nvarchar(3)) = ap.value
where e.EnvType = 0
Basically the value field will have strings like “1”, “2” etc. So its alright if I could do either string to string comparison or int to int conversion. Bottom line is "1" = 1 and "2" = 2 and so on.
Edit: CAST(e.FK_Qid as char3) = ap.value worked as well..
If
ap.valuehas only values like “1”, “2”…, then you can use your first query, server will do everething itself. All values will be converted to numbers, and your ON condition will work.