3

Here is what I am trying to do:

SELECT iif(d.ItemType = 'INVOICE', 0, iif(d.ItemType = 'PACKING', 0, 1)) AS MissingDocuments FROM dbo.DocumentDetails AS D

Unfortunately realized this is not compatible with MSSQL2008. So tried writing an IF ELSE but that also is not working.

SELECT  IF d.ItemType = 'INVOICE'
   0
ELSE 
   BEGIN
      d.ItemType = 'PACKING'
      0
   ELSE
      1
   END  AS MissingDocuments
FROM  dbo.DocumentDetails AS D 

can you please tell me what am I doing wrong here ?

6
  • 2
    use CASE. CASE WHEN d.ItemType IN ('INVOICE','PACKING') THEN 0 ELSE 1 END Commented May 19, 2015 at 5:39
  • you're missing if statement in your else block. There is no corresponding if for your nested else Commented May 19, 2015 at 5:41
  • possible duplicate of stackoverflow.com/questions/4622/sql-case-statement-syntax Commented May 19, 2015 at 5:42
  • @ughai You should have posted an answer...those points could have been yours! Commented May 19, 2015 at 5:47
  • 3
    @TimBiegeleisen - As long as OP gets an answer and the question gets closed, it's all good. :) Commented May 19, 2015 at 5:52

2 Answers 2

7

Use CASE ... WHEN. The most concise logic seems to be:

SELECT 
  CASE WHEN d.ItemType IN ('INVOICE', 'PACKING') THEN 0 ELSE 1 END
     AS MissingDocuments 
FROM dbo.DocumentDetails AS d

i.e. the Document is missing if it isn't 'Invoice' or 'Packing'

Sign up to request clarification or add additional context in comments.

Comments

2

I think You Looking For Case When Try This..

SELECT 
 case when d.ItemType = 'INVOICE' or d.ItemType = 'PACKING'
 then  0
 ELSE
  1
 END  AS MissingDocuments
FROM  dbo.DocumentDetails AS D 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.