Use CASE statement to assign a value to a specified field in Oracle

Source: Internet
Author: User

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

 
 
  1. create table t(  
  2. id integer,  
  3. name varchar2(50)  

2. Add data

 
 
  1. insert into t values(1,'zhao')  
  2. insert into t values(2,'qian')  
  3. insert into t values(3,'sun')  
  4. insert into t values(4,'li') 

Iii. case statement usage: assign a new value to a specified field

Example 1:

 
 
  1. Select t. Name,
  2. (Case t. id -- parentheses can be omitted here, And the id is changed to name
  3. When 1 then 'zhao'
  4. When 2 then 'Qian'
  5. When 3 then 'sun'
  6. Else 'lil'
  7. End) -- Do not discard the end keyword.
  8. From T t

Extension: assign a value to a specified field again. Another method is decode () usage.

 
 
  1. select name, decode(id,1,'zhao',2,'qian',3,'sun','li') from T 

Example 2:

 
 
  1. Select name,
  2. Case
  3. When id <= 2 then 0
  4. When id = 3 then 1
  5. Else 2
  6. End "targer" -- defines an alias. Here, it is a double quotation mark.
  7. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.