Search code examples
sql-serverexcelsql-server-2000vba

SQL Server 2000 : return a value when no record is returned


I have a list of about 1500 part numbers in an Excel document. I want to insert these part numbers in a query to pull descriptions from my database and then copy/paste the results back into the Excel document.

The problem is some of the part numbers in my list aren't in my database, so when I go to copy/paste the results of my query into Excel they aren't going to match up because some of the part numbers in the query do not return a result.

SELECT ITEM.ITEMNO, ITEM.DESCRIPTION 
FROM ITEMS 
WHERE ITEM.ITEMNO IN ('1500','Part','Numbers')

It seems like a simple concept and my google searches have returned a lot of results, but they usually involve multiple tables and unions/joins. I just can't translate the examples I have seen to my query. If VBA is a better option then I am all ears.


Solution

  • select a.ITEMNO, IFNULL(i.DESCRIPTION, 'N/A')
    from (
        select '1500' as ITEMNO
        union all select 'Part'
        union all select 'Numbers'
    ) a
    left outer join ITEMS i on a.ITEMNO = i.ITEMNO