Home > Database > Oracle > casewhen usage in oracle

casewhen usage in oracle

下次还敢
Release: 2024-04-30 06:57:14
Original
454 people have browsed it

The CASE WHEN statement in Oracle is used to return different values ​​based on conditions. Syntax: CASE WHEN condition THEN result ELSE default_result END. Usage: 1. Condition check: WHEN clause contains the condition and returned result; 2. Default result: ELSE clause specifies the default result when any condition is not met. Example: Return income_level based on salary value: salary > 5000: high income; 3000 ≤ salary ≤ 5000: middle income; salary < 3000: low income.

casewhen usage in oracle

CASE WHEN usage in Oracle

The CASE WHEN statement is a conditional expression that is used to Group conditions return different values. It is very useful when dealing with complex data queries and operations.

Syntax:

<code class="sql">CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    ...
    ELSE default_result
END</code>
Copy after login

Usage:

  1. Conditional check: CASE statement begins with Begins with a series of WHEN clauses, each containing a condition and a result to be returned.
  2. Default result: The ELSE clause is optional and is used to specify the default result when all conditions are not met.

Example:

SELECT CASE
    WHEN salary > 5000 THEN '高收入'
    WHEN salary BETWEEN 3000 AND 5000 THEN '中等收入'
    ELSE '低收入'
END AS income_level
FROM employees;

Result:

This query will return the income_level column based on the employee’s salary value:

  • salary > 5000: high income
  • 3000 ≤ salary ≤ 5000: middle income
  • salary < 3000: low income

Note:

  • The conditions in the CASE statement can use any valid SQL expression.
  • CASE statements can be nested, allowing the creation of more complex branching logic.
  • In some cases, the CASE statement can be replaced by the DECODE function, which has a more concise syntax.

The above is the detailed content of casewhen usage in oracle. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template