Shivang Shivang - 3 months ago 6
SQL Question

SQL Server query error for conversiontype

I am getting an error in SQL Server for the code.

SQL code:

SELECT DISTINCT
HEADER.PONUMBER,
SName,
CompanyName
FROM
HEADER
INNER JOIN
PDetails ON HEADER.PONUMBER = PDetails.PoNumber
INNER JOIN
FAN_P ON PDetails.PurchaseOrderID = FAN_P.PurchaseOrderID


Error message:


Conversion failed when converting the varchar value 'PO321211' to data type int.


Could you please me with this?

Thanks!

Answer

The error is clear. You are trying to compare and INT column to a VARCHAR column. To alleviate this, you can cast or convert your comparisons to varchar. I did them all since I don't know which is causing the error... but it's the column with PO321211 in it :)

SELECT  DISTINCT  HEADER.PONUMBER, 
                  SName,
                  CompanyName
FROM HEADER INNER JOIN PDetails 
ON CAST(HEADER.PONUMBER AS VARCHAR(128)) = CAST(PDetails.PoNumber AS VARCHAR(128))
INNER JOIN FAN_P ON CAST(PDetails.PurchaseOrderID AS VARCHAR(128)) = CAST(FAN_P.PurchaseOrderID AS VARCHAR(128)) 

https://msdn.microsoft.com/en-us/library/ms187928.aspx

Comments