Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Conditional expressions

181 views

Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

Conditional expressions

  1. 1. Conditional ExpressionsConditional ExpressionsExpressions are building blocks for creating conditional expressions that convert a value from one form toanother. Expressions include:· CASE (Switch)· CASE (If)CASE (Switch)This form of the CASE statement is also referred to as the CASE(Lookup) form. The value of expr1 isexamined, then the WHEN expressions. If expr1matches any WHEN expression, it assigns the value in thecorresponding THEN expression.If none of the WHEN expressions match, it assigns the default value specified in the ELSE expression. Ifno ELSE expression is specified, the system automatically adds an ELSE NULL.If expr1 matches an expression in multiple WHEN clauses, only the expression following the first match isassigned.SyntaxCASE expr1 WHEN expr2 THEN expr3 {WHEN expr... THEN expr...} ELSE exprENDWhere:CASE starts the CASE statement. Must be followed by an expression and one ormore WHEN and THEN statements, an optional ELSE statement, and the END keyword.WHEN specifies the condition to be satisfied.THEN specifies the value to assign if the corresponding WHEN expression is satisfied.ELSE specifies the value to assign if none of the WHEN conditions are satisfied. If omitted, ELSE NULL isassumed.END ends the CASE statement.ExampleCASE "TableHeading"."Column Name"WHEN Col_Val1 THEN Val1
  2. 2. WHEN Col_Val2 THEN Val2WHEN Col_Val3 THEN Val3ELSE "TableHeading"."Column Name"ENDIn this example, the WHEN statements must reflect a strict equality.CASE (If)This form of the CASE statement evaluates each WHEN condition and if satisfied, assigns the value in thecorresponding THEN expression.If none of the WHEN conditions are satisfied, it assigns the default value specified in the ELSE expression. Ifno ELSE expression is specified, the system automatically adds an ELSE NULL.SyntaxCASE WHEN request_condition1 THEN expr1 {WHEN request_condition2 THEN expr2} {WHEN request_condition... THEN expr...} ELSE exprENDWhere:CASE starts the CASE statement. Must be followed by one or more WHEN and THEN statements, anoptional ELSE statement, and the END keyword.WHEN specifies the condition to be satisfied.THEN specifies the value to assign if the corresponding WHEN expression is satisfied.ELSE specifies the value to assign if none of the WHEN conditions are satisfied. If omitted, ELSE NULL isassumed.END ends the CASE statement.ExampleCASEWHEN ("TableHeading"."ColumnName1"=Collaboration AND "TableHeading"."ColumnName"=Value1)THEN Val1WHEN ("TableHeading"."ColumnName1"=Collaboration AND"TableHeading1"."ColumnName2"=Value2) THEN Val2WHEN "TableHeading"."Column Name"=Value3 THEN Val3ELSE "TableHeading"."Column Name"
  3. 3. ENDNote :Unlike the Switch form of the CASE statement, the WHEN statements in the If form allow comparison operators.In a CASE statement, AND has precedence over OR.

×