troubleshooting Question

Compare a numeric value to a varchar w/o getting an error

Avatar of lburnsinmagnolia
lburnsinmagnolia asked on
Microsoft SQL Server
4 Comments1 Solution64 ViewsLast Modified:
I have the following query

      select si.OrderID, SO.ORDERDATE, QtyOrdered
, si.LineCost, si.ProductDesc, si.BrandID, SI.CaseSellingPrice
      fROM SAMSDW.storis.OrderItem SI                                                
      JOIN SAMSDW.storis.Orders SO ON
            SO.OrderID = SI.OrderID
      WHERE SI.OrderID = '300119'
      and si.ProductID not in (select itemid from INVENTORY where itemid = CASE WHEN ISNUMERIC(SI.PRODUCTID) = 1 THEN CONVERT(NUMERIC(18,0), SI.PRODUCTID) ELSE 0 END)

ITEMID is a numeric(18,0) in INVENTORY and PRODUCTID is varchar(50) in SAMSDW.  Sometimes, the SAMSDW table will be a number, most of time it will not.  I merely want to test the 2 values by converting/casting the PRODUCTID if it's numeric.  But I crash no matter what.

Is there any way to get around the error?

Database Consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2009

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros