many of the developers are came up with these error ” Conversion failed when converting the varchar value to data type int” while working with SQL server. Even myself spent alot of time to find out the exact solution for this issue . Finally I got the solution and also I am sharing my findings below . This may help my friends as well those who are straggling with the same issue.
DECLARE @strValue VARCHAR(250) SELECT @strValue = COALESCE(@strValue + ',', '') + strId FROM tbl_emp SELECT @strValue
To solve this problem we need to convert that integer value to string explicitly like as shown below
DECLARE @strValue VARCHAR(250) SELECT @strValue = COALESCE(@strValue + ',', '') +CAST(strId AS VARCHAR(15)) FROM tbl_emp SELECT @strValue