In Oracle, the when statement is often used in conjunction with case to determine the situation of multiple fields. The syntax is "case column name when condition value 1 then option 1 when condition value 2 then option 2... else default value end".
The operating environment of this tutorial: Windows 10 system, Oracle version 12c, Dell G3 computer.
oracle case when usage
Case has two formats. Simple Case function and Case search function.
Usage of Case when in Oracle:
(a) Starts with case and ends with end;
(b) When is followed by condition, then displays the result ;
(c) else is the default case, similar to the default of switch case in high-level language programs, which can be omitted;
(d) end is followed by an alias;
Case has two kinds of expressions:
(A) Simple case expressions use expressions to determine the return value;
(B) Search case expressions and use conditions to determine the return value Value;
Example:
select (case t.sex when '1' then '男' when '0' then '女' else t.sex end from emp t; select (case when t.sex ='1' then '男' when t.sex= '0' then '女'else t.sex end from emp t;
We use case when to convert the sex of 1 into male, the sex of 0 into female, and the others None of them are converted. Now let's change the code a little and see the effect.
In this way, when the gender code is 1 or 0, it is converted into male or female. If it is not 1 or 0, it is displayed as an error code.
Recommended tutorial: "Oracle Video Tutorial"
The above is the detailed content of How to use when statement in oracle. For more information, please follow other related articles on the PHP Chinese website!