Preamble
Oracle/PLSQL DECODE function has functionality of IF-THEN-ELSE operator.
Oracle/PLSQL syntax of the DECODE function
DECODE( expression_id , search_id , result_id [, search , result]... [, default] )
Parameters or arguments
- expression_id – is an expression for comparison.
- search_id – value that is compared to.
- result_id – value returned if the expression coincided with the sought search.
default – optional. If no matches are found, the DECODE function will return the default value. If no match is found, DECODE will return NULL (if no match is found).
DECODE in the following versions of Oracle/PLSQL
| Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i |
You can use the DECODE function in an SQL query as follows:
SELECT suppl_name,
DECODE(suppl_id, 10000, 'IBM',
10001, 'Microsoft',
10002, 'Hewlett Packard',
'Gateway') result
FROM suppls;
Request using DECODE is equivalent to the IF-THEN-ELSE design.
IF suppl_id = 10000 THEN
result := 'IBM';
ELSIF suppl_id = 10001 THEN
result := 'Microsoft';
ELSIF suppl_id = 10002 THEN
result := 'Hewlett Packard';
ELSE
result := 'Gateway';
END IF;
The DECODE function will compare each suppl_id value, one after the other.
Frequently Asked Questions
Question:
One of our readers wanted to know how to use the DECODE function to compare two dates (that is: date1 and date2), where date1 > date2, the DECODE function should return date2. Otherwise, the DECODE function should return date1.
Answer:
To do so, use the DECODE function as follows:
DECODE((date1 - date2) - ABS(date1 - date2), 0, date2, date1)
The formula below is 0 if date1 is greater than date2:
(date1 - date2) - ABS(date1 - date2)
Useful advice No. 1:
One of our readers suggested combining the SIGN function with the DECODE function as follows:
The example with the dates above can be modified as follows:
DECODE(SIGN(date1-date2), 1, date2, date1)
The combination of SIGN / DECODE is also useful for digital comparisons such as bonus sales.
DECODE(SIGN(actual-target), -1, ‘No bonuses for you’, 0, ‘Just do it’, 1, ‘Congratulations, you are the winner’)
Useful advice No. 2:
One of our readers suggested using the LEAST function (instead of DECODE) as follows:
An example with dates above can be modified as follows:
LEAST(date1, date2)
Question:
I would like to know if it is possible to use the DECODE function for number ranges, i.e. 1-10 = ‘category 1’, 11-20 = ‘category 2’, instead of decoding each number individually.
The answer is:
Unfortunately, you cannot use the DECODE function for number ranges. However, you can try to create a formula that will define one number for one range and another number for another range, and so on.
For example:
SELECT suppl_id,
DECODE(TRUNC ((suppl_id - 1) / 10), 0, 'cat 1',
1, 'cat 2',
2, 'cat 3',
'unknown' result
FROM suppls;
This example, based on a formula:
- TRUNC((suppl_id – 1) / 10)
- The formula will score 0 if suppl_id is between 1 and 10.
- The formula will be evaluated as 1 if suppl_id is between 11 and 20.
- The formula will be evaluated at 2 if suppl_id is between 21 and 30.
Question:
I need to write a DECODE request that will return the following:
If yrs_of_service <1, then return 0.04 If yrs_of_service> = 1 and <5, then return 0.04 If yrs_of_service> 5, then return 0.06
How can I do that?
The answer:
You will need to create a formula that calculates a unit number for each of your ranges.
For example:
SELECT emp_name,
DECODE(TRUNC ((yrs_of_service + 3) / 4), 0, 0.04,
1, 0.04,
0.06) as perc_value
FROM empls;
Question:
Is there a limit on the number of arguments that you can specify in one DECODE operator? I get the error message “ORA-00939: too many arguments for the function”.
The answer is:
Yes, the maximum number of components that you can have in a DECODE function is 255. This includes, search and result arguments.
SQL tutorial: DECODE function in Oracle Database
About Enteros
Enteros offers a patented database performance management SaaS platform. It finds the root causes of complex database scalability and performance problems that affect business across a growing number of cloud, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
How to Drive Intelligent Cloud Governance with Enteros Database Management Platform and AIOps
- 22 May 2026
- Database Performance Management
Introduction Cloud computing has become the foundation of modern digital transformation. Organizations across industries increasingly rely on cloud-native infrastructures, distributed applications, AI-driven services, and real-time analytics platforms to support innovation, scalability, and operational agility. Today’s enterprises operate highly complex cloud ecosystems that support: Business-critical applications Database environments Customer engagement platforms AI and machine learning workloads … Continue reading “How to Drive Intelligent Cloud Governance with Enteros Database Management Platform and AIOps”
How to Enhance Media Growth Strategies with Enteros Generative AI and Cost Management Analytics
Introduction The media industry is undergoing rapid transformation as organizations embrace digital platforms, cloud-native infrastructures, streaming technologies, AI-driven content strategies, and real-time audience analytics. Modern media companies must manage increasingly complex operational ecosystems while delivering personalized, high-quality experiences across multiple digital channels. Today’s media organizations rely heavily on digital technologies to support: Streaming platforms Content … Continue reading “How to Enhance Media Growth Strategies with Enteros Generative AI and Cost Management Analytics”
Enhancing Digital Banking Performance and Scalability with AI-Driven Database Analytics
- 21 May 2026
- Database Performance Management
Introduction Digital banking has transformed the global financial landscape. Customers now expect instant account access, real-time transactions, personalized financial services, and seamless digital experiences across mobile and web platforms. To meet these expectations, banks and financial institutions rely heavily on high-performance data infrastructure powered by complex database environments. Every digital banking operation—whether it involves payments, … Continue reading “Enhancing Digital Banking Performance and Scalability with AI-Driven Database Analytics”
How Intelligent Database Analytics Improves Performance and Reliability in Modern Healthcare Platforms
Introduction Healthcare organizations today operate in an increasingly data-driven environment. Hospitals, clinics, diagnostic centers, telemedicine platforms, and healthcare networks rely heavily on digital systems to manage patient records, medical imaging, billing systems, analytics platforms, and clinical workflows. At the center of these operations lies a complex healthcare data infrastructure powered by databases. These databases process … Continue reading “How Intelligent Database Analytics Improves Performance and Reliability in Modern Healthcare Platforms”