In Oracle, the decode() function is used to compare the input value with the parameter list in the function, and return a corresponding value based on the input value. The syntax is "decode(condition, value 1, return value 1 , value 2, return value 2,... value n, return value n, default value)".

The operating environment of this tutorial: Windows 10 system, Oracle 11g version, Dell G3 computer.
What is the usage of decode in oracle
Oracle function decode usage:
decode(条件,值1,返回值1,值2,返回值2,…值n,返回值n,缺省值);
This is the expression of decode.
The DECODE() function compares the input value with the parameter list in the function and returns a corresponding value based on the input value. The parameter list of a function is composed of several numerical values and their corresponding result values. Several ordinal even forms. Of course, if it fails to successfully match any of the actual parameter sequences, the function also has a default return value.
The meaning of this function is as follows:
IF 条件=值1 THEN RETURN(翻译值1) ELSIF 条件=值2 THEN RETURN(翻译值2) ...... ELSIF 条件=值n THEN RETURN(翻译值n) ELSE RETURN(缺省值) END IF
decode (field or field operation, value 1, value 2, value 3)
The result of this function is, when When the value of the field or field operation is equal to value 1, the function returns value 2, otherwise it returns value 3
Of course value 1, value 2, and value 3 can also be expressions. This function makes certain sql statements Much simpler
Usage method:
1. Compare size
select decode(sign(variable 1-variable 2),-1,variable 1,variable 2) from dual; --Take the smaller value
The sign() function returns 0, 1, and -1 respectively depending on whether a value is 0, a positive number, or a negative number
For example:
Variable 1=10, variable 2=20
Then sign (variable 1-variable 2) returns -1, and the decode decoding result is "variable 1", achieving the purpose of taking a smaller value.
2. This function is used in SQL statements. The function is introduced as follows:
The Decode function is similar to a series of nested IF-THEN-ELSE statements. base_exp is compared with compare1, compare2, etc. in sequence. If base_exp matches the i-th compare item, return the i-th corresponding value. If base_exp does not match any compare value, default is returned. Each compare value is evaluated in sequence, and if a match is found, the remaining compare values (if any) are not evaluated again. A NULL base_exp is considered equivalent to a NULL compare value. If necessary, each compare value is converted to the same data type as the first compare value, which is also the type of the return value.
The Decode function is very useful in actual development
Combined with the Lpad function, how to automatically add 1 to the value of the primary key and add 0 in front
select LPAD(decode(count (record number),0,1,max(to_number(record number) 1)),14,'0') The value of record number from tetdmis
eg:
select decode(dir,1,0,1) from a1_interval
dir is 1 becomes 0, and if it is 0, it becomes 1
For example, I want to query the number of boys and girls in a certain class?
Usually we write like this:
select count(*) from 表 where 性别 = 男; select count(*) from 表 where 性别 = 女;
To If you want to display them together, you have to union them, which is too troublesome.
Use decode. You only need one sentence
select decode(gender, male, 1, 0), decode(gender, female, 1, 0) from table
3, order by to sort the character column in a specific way
You can also use Decode in Order by.
Example: Table table_subject has a subject_name column. It is required to sort them in the order of: language, number, and foreign language. At this time, you can easily use Decode to complete the requirements.
select * from table_subject order by decode(subject_name, '语文', 1, '数学', 2, , '外语',3)
Recommended tutorial: "Oracle Video Tutorial"
The above is the detailed content of What is the usage of decode in oracle. For more information, please follow other related articles on the PHP Chinese website!
What Does Oracle Offer? Products and Services ExplainedApr 16, 2025 am 12:03 AMOracleoffersacomprehensivesuiteofproductsandservicesincludingdatabasemanagement,cloudcomputing,enterprisesoftware,andhardwaresolutions.1)OracleDatabasesupportsvariousdatamodelswithefficientmanagementfeatures.2)OracleCloudInfrastructure(OCI)providesro
Oracle Software: From Databases to the CloudApr 15, 2025 am 12:09 AMThe development history of Oracle software from database to cloud computing includes: 1. Originated in 1977, it initially focused on relational database management system (RDBMS), and quickly became the first choice for enterprise-level applications; 2. Expand to middleware, development tools and ERP systems to form a complete set of enterprise solutions; 3. Oracle database supports SQL, providing high performance and scalability, suitable for small to large enterprise systems; 4. The rise of cloud computing services further expands Oracle's product line to meet all aspects of enterprise IT needs.
MySQL vs. Oracle: The Pros and ConsApr 14, 2025 am 12:01 AMMySQL and Oracle selection should be based on cost, performance, complexity and functional requirements: 1. MySQL is suitable for projects with limited budgets, is simple to install, and is suitable for small to medium-sized applications. 2. Oracle is suitable for large enterprises and performs excellently in handling large-scale data and high concurrent requests, but is costly and complex in configuration.
Oracle's Purpose: Business Solutions and Data ManagementApr 13, 2025 am 12:02 AMOracle helps businesses achieve digital transformation and data management through its products and services. 1) Oracle provides a comprehensive product portfolio, including database management systems, ERP and CRM systems, helping enterprises automate and optimize business processes. 2) Oracle's ERP systems such as E-BusinessSuite and FusionApplications realize end-to-end business process automation, improve efficiency and reduce costs, but have high implementation and maintenance costs. 3) OracleDatabase provides high concurrency and high availability data processing, but has high licensing costs. 4) Performance optimization and best practices include the rational use of indexing and partitioning technology, regular database maintenance and compliance with coding specifications.
How to delete oracle library failureApr 12, 2025 am 06:21 AMSteps to delete the failed database after Oracle failed to build a library: Use sys username to connect to the target instance. Use DROP DATABASE to delete the database. Query v$database to confirm that the database has been deleted.
How to create cursors in oracle loopApr 12, 2025 am 06:18 AMIn Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.
How to export oracle viewApr 12, 2025 am 06:15 AMOracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.
How to stop oracle databaseApr 12, 2025 am 06:12 AMTo stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),







