user2975266
user2975266

Reputation: 67

IF/CASE Statement SQL

I am unsure how to code this. Here are the scenarios I am needing in the IF statement. Should this be broken up into two different IF statements or could I possibly use CASE? I am new to programming so any help would be appreciated.

Scenerios: IF selling_flag = 'N' and calculation_flag = 'Y' THEN return list_price. IF selling_flag = 'Y' and calculation_flag = 'Y' THEN return customer_price. IF calculation_flag = 'N' THEN return unit_selling

Currently I have:

IF selling_flag = 'N' AND calculation_flag = 'Y' THEN
  return list_price;
ELSE  
  return customer_price;      
END IF; 

IF calculation_flag = 'N' THEN
return selling_price;
END IF; 
END;

Thank you.

Upvotes: 0

Views: 101

Answers (2)

Dresden
Dresden

Reputation: 559

You could potentially do it this way Case Statements PLSQL:

CASE 
    WHEN selling_flag = 'N' AND calculation_flag = 'Y' THEN list_price;
    WHEN selling_flag = 'Y' AND calculation_flag = 'Y' THEN customer_price;
    ELSE selling_price;
END CASE;

Where you have your two conditions and if neither are met it defaults to selling_price.

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1271151

If this is PL/SQL code, you would normally use elsif:

IF selling_flag = 'N' AND calculation_flag = 'Y' THEN
  return list_price;
ELSIF selling_flag = 'Y' AND calculation_flag = 'Y'
  return customer_price;      
ELSIF calculation_flag = 'N' THEN
  return selling_price;
END IF; 

This might also be written as:

IF calculation_flag = 'Y' THEN
  IF selling_flag = 'N' THEN
    return list_price;
  ELSE 
    return customer_price;
  END IF;     
ELSE
  return selling_price;
END IF; 

Upvotes: 1

Related Questions