Home  >  Article  >  Database  >  How to use case in oracle

How to use case in oracle

WBOY
WBOYOriginal
2022-08-02 16:15:567134browse

In Oracle, the case statement is used to compare the expression result with several provided foreseeable results. If the comparison is successful, the corresponding statement sequence is executed. The syntax is "CASE expression WHEN expression result 1 THEN executable [WHEN expression result 2 THEN executable]...".

How to use case in oracle

The operating environment of this tutorial: Windows 10 system, Oracle version 12c, Dell G3 computer.

How to use case in oracle

This statement can compare the expression result with several foreseeable results provided. If the comparison is successful, the corresponding statement sequence will be executed. The syntax is " CASE expression WHEN expression result 1 THEN execution item [WHEN expression result 2 THEN execution item]...".

There are case statements in oracle

case statements can be divided into two types:

One is a simple case statement, which gives a Expression, and compares the expression result with several provided predictable results. If the comparison is successful, the corresponding statement sequence is executed.

The other is the search-style case statement. It will provide multiple Boolean expressions, then select the first expression that is true and execute the corresponding script.

1. Simple case statement

Syntax:

case exp when comexp then returnvalue
…
when comexp then returnvalue
else returnvalue
end

The period from case to end is equivalent to a specific value, which can be used for calculations and aliases , nested cases, etc.

Just treat case to end as an expression of the operation result.

Example:

How to use case in oracle

2. Search case statement

Grammar:

case when boolean then return value
…
when boolean then return value
else retur nvalue
end

Example :

How to use case in oracle

The difference between simple case and search case:

  • Simple case can only be when The following expression completely matches the expression after the case, which is equivalent to =, so it cannot match null.

  • searched case can be used as a comparison condition, then you can use like, !=, between...and,

Recommended tutorial: "Oracle Video Tutorial"

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

Statement:
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