I am getting 'Conversion failed when converting the nvarchar value 'XXX' to data type int' error for no obvious reason and I don't know how to fix it. The view actually returns the correct data to stored procedure but it is giving me this error. I'm stuck. Anyone please give me some pointers?
SELECT d.Code, a.Agency, a.Agency_job, a.OUCode, a.JobCode, d.OraUser, c.ProfileName
FROM dbo.Agency AS a
LEFT OUTER JOIN dbo.IdM AS d ON REPLACE(STR(a.OUCode, 3), ' ', '0') = d.OrganizationUnitCode AND a.JobCode = d.JobCode
LEFT OUTER JOIN dbo.Profiles AS c ON c.JobCode = a.JobCode AND c.OUCode = a.OUCode
WHERE (d.Code IS NOT NULL)
Column details -
d.Code is nvarchar(32),
d.OraUser is nvarchar(100),
d.OrganizationUnitCode is nvarchar(16),
a.Agency is int,
a.Agency_job is int,
a.OUCode is int,
a.JobCode is int,
c.ProfileName is varchar(100),
c.OUCode is int,
c.JobCode is int.
Does anyone maybe see a potential problem?