Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

Oracle DECODE function

23 July 2020

Oracle DECODE function

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

 
Tags: , , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...