CASE statementInOracleRe-runningAssign a value to a specified fieldThe usage is what we will introduce in this article. First we need to create a database and add data. Then we will introduce this usage using two instances. Next we will introduce this part of content.
1. Create a data table
- create table t(
- id integer,
- name varchar2(50)
- )
2. Add data
- insert into t values(1,'zhao')
- insert into t values(2,'qian')
- insert into t values(3,'sun')
- insert into t values(4,'li')
Iii. case statement usage: assign a new value to a specified field
Example 1:
- Select t. Name,
- (Case t. id -- parentheses can be omitted here, And the id is changed to name
- When 1 then 'zhao'
- When 2 then 'Qian'
- When 3 then 'sun'
- Else 'lil'
- End) -- Do not discard the end keyword.
- From T t
Extension: assign a value to a specified field again. Another method is decode () usage.
- select name, decode(id,1,'zhao',2,'qian',3,'sun','li') from T
Example 2:
- Select name,
- Case
- When id <= 2 then 0
- When id = 3 then 1
- Else 2
- End "targer" -- defines an alias. Here, it is a double quotation mark.
- From T
The above is the usage of the CASE statement to assign a value to a specified field in Oracle. This article will introduce it here. I hope this introduction will help you.