So I have this query
select *
FROM
[JMNYC-AMTDB].[AMTPLUS].[dbo].PickTickets i WITH (NOLOCK)
INNER JOIN
[JMNYC-AMTDB].[AMTPLUS].[dbo].customer_store cs WITH (NOLOCK)
ON i.Company_code = cs.Company_code
AND i.Division_code = cs.Division_code
AND i.Customer_Number = cs.Customer_Number
AND i.ShipTo=cs.store_number
JOIN
[JMNYC-AMTDB].[AMTPLUS].[dbo].PickTickets_Packing ps1 WITH (NOLOCK)
ON i.Company_Code=ps1.Company_Code
AND i.Division_Code=ps1.Division_Code
AND i.PickTicket_Number=ps1.PickTicket_Number
JOIN
[JMNYC-AMTDB].[AMTPLUS].[dbo].Orders o WITH (NOLOCK)
ON i.Company_Code=o.Company_Code
AND i.Division_Code=o.Division_Code
AND i.Control_Number=o.Control_Number
LEFT JOIN
[JMNYC-AMTDB].[AMTPLUS].[dbo].Country cr WITH (NOLOCK)
ON cs.country =cr.country
LEFT JOIN
Packslip P ON I.COMPANY_CODE=P.CLIENTNAME
LEFT JOIN
Moret_shipper sh ON I.COMPANY_CODE=sh.CLIENTNAME
LEFT JOIN
CUSTOMER cu ON i.Customer_Number=cu.CUST_NUM
LEFT JOIN
(
SELECT packslip FROM PICKHEAD (NOLOCK)
WHERE CLIENTNAME='03'
) td3 ON I.PickTicket_number=td3.packslip
JOIN
[JMNYC-AMTDB].[AMTPLUS].[dbo].Picktickets_stage pst1 WITH (NOLOCK)
ON i.Company_Code=pst1.Company_Code
AND i.Division_Code=pst1.Division_Code
AND i.PickTicket_Number=pst1.PickTicket_Number
AND pst1.Stage_code='940READY'
LEFT JOIN
(
SELECT packslip FROM rf_log_all (NOLOCK)
WHERE action='DELETESO' AND clientname='03'
) td4 ON I.PickTicket_number=cast(td4.packslip as numeric)
If I run it just like that, everything seems to work fine.
However as soon as I start adding the where clause, I start getting a weird conversion error.
Here is my where clause:
WHERE
i.company_code='03'
AND ps1.Packed_Status='E'
AND i.Warehouse_Code in ('DAYTO','SANFE','ALLLM')
AND td3.packslip is null
AND td4.packslip is null
With that I get the Error converting data type nvarchar to numeric.
If I comment out the td3 and td4 checks, it works.
WHERE
i.company_code='03'
AND ps1.Packed_Status='E'
AND i.Warehouse_Code in ('DAYTO','SANFE','ALLLM')
--AND td3.packslip is null
--AND td4.packslip is null
However, it also works with those left in but the company code commented out.
WHERE
--i.company_code='03' AND
ps1.Packed_Status='E'
AND i.Warehouse_Code in ('DAYTO','SANFE','ALLLM')
AND td3.packslip is null
AND td4.packslip is null
Does anyone know what might be causing this error. This is a query that we've been using for a while and this problem only started recently. If I knew what specific line is causing this error, I could try to fix it, but I'm not even sure of that.
WHEREand which work (and which don't) doesn't really make sense though, in the case ofpackslip IS NULLthe data typepackslipis meaningless. I would more expect the issue to be withi.company_code='03'. Really we need DDL of all your tables, but best I can say is that somewhere you are comparing a numerical data type to a string datatype. (ON a different note, do you *really need every column back from your query here? Seems a bit excessive seeing as some will be duplicated by theJOIN.)*