I am trying to run this query but is not working. I am getting an error. How to fix?:
Conversion failed when converting the nvarchar value ‘Jon Yang ‘ to data type int.
sql
use adventureworks
go
select si.CustomerID,
'myField' =
CASE
When (Select Top 1 FirstName+ ' ' + LastName + ' ' + EmailPromotion From Person.Contact pc Where si.ContactID = pc.contactid ) is not null Then
Cast((Select Top 1 FirstName+ ' ' + LastName + ' ' + EmailPromotion From Person.Contact pc Where si.ContactID = pc.contactid ) As varchar)
Else ''
END
from Sales.Individual si
where si.CustomerID=11000
The error occurs because of datatype precedence: nvarchar will be converted to int.
It is either on
EmailPromotionorCustomerIDso decide which line you want below.Also, no need to use an inline query.
TOP without ORDER BY is meaningless, so if you have multiple rows in
Person.Contactfor each row inSales.Individualyou’d need another construct…