The equivalent of Oracle's decode function in MySQL
This article presents three alternative implementations that we can use as the equivalent of Oracle's decode() function in MySQL. To do this, we will use IF(), CASE, and a combination of FIELD() and ELT().
MySQL equivalent of Oracle's decode function
MySQL has its decode()
function, but we will look at decode()
an implementation that can be used as an alternative to Oracle's function.
Before jumping into the implementation, it is good to understand the difference between MySQL decode()
functions and Oracle functions.decode()
MySQL decode() and Oracle decode()
The MySQL decode()
function decodes the encoded string and outputs the original string. The Oracle decode() function compares the expression with each search term one by one.
If the expression is equal to the search value, Oracle Database will output the corresponding result. In Oracle decode(), if no match is found, it returns the default value, and if there is no default value, it returns NULL.
To practice it with code examples, we must first have a table. We create a table called users with ID, USERNAME, and LEVELS as attributes.
We also insert some data to learn clearly. In this tutorial, you can create and populate a table using the query given below to continue our learning.
Sample code (create and populate table):
# Create a table
CREATE TABLE users(
ID INT NOT NULL AUTO_INCREMENT,
USERNAME VARCHAR(45) NOT NULL,
LEVELS INT NOT NULL,
PRIMARY KEY (ID));
# Insert data
INSERT INTO users (USERNAME, LEVELS) VALUES
('mehvishashiq', 1),
('thomas011', 2),
('danielchristopher', 3),
('sairajames', 4);
SELECT * FROM users;
Output:
| ID | USERNAME | LEVELS |
| ---- | ----------------- | ------ |
| 1 | mehvishashiq | 1 |
| 2 | thomas011 | 2 |
| 3 | danielchristopher | 3 |
| 4 | sairajames | 4 |
Using CASE to simulate Oracle's decode() function in MySQL
Sample code:
SELECT USERNAME,
CASE
WHEN LEVELS = 1 THEN 'Level One'
WHEN LEVELS = 2 THEN 'Level Two'
WHEN LEVELS = 3 THEN 'Level Three'
WHEN LEVELS = 4 THEN 'Level Four'
ELSE 'Beginner Level'
END AS USER_LEVEL
FROM users;
Output:
| USERNAME | USER_LEVEL |
| ----------------- | ----------- |
| mehvishashiq | Level One |
| thomas011 | Level Two |
| danielchristopher | Level Three |
| sairajames | Level Four |
The CASE statement also emulates Oracle's decode() function, comparing each expression to each search value and returning the corresponding result. Here, the default value of Beginner Level is returned if no match is found.
Using IF() in MySQL to simulate Oracle's decode() function
If we wish to split the result into binary values, we can use IF().
Sample code:
SELECT USERNAME, IF(LEVELS >= 3,'Top Rates Plus','Top Rated') AS USER_LEVEL from users;
Output:
| USERNAME | USER_LEVEL |
| ----------------- | -------------- |
| mehvishashiq | Top Rated |
| thomas011 | Top Rated |
| danielchristopher | Top Rated Plus |
| sairajames | Top Rated Plus |
Combining FIELD and ELT to simulate Oracle's decode() function in MySQL
Sample code:
SELECT USERNAME,
IFNULL(ELT( FIELD(LEVELS,1,2,3,4),
'Level One', 'Level Two',
'Level Three', 'Level Four'
),'Beginner Level')
As USER_LEVEL FROM users;
Output:
| USERNAME | USER_LEVEL |
| ----------------- | ----------- |
| mehvishashiq | Level One |
| thomas011 | Level Two |
| danielchristopher | Level Three |
| sairajames | Level Four |
Here, FIELD() outputs the position in the argument list of the string that matches LEVELS. Now, ELT() outputs a string from the argument list of ELT() at the position specified by FIELD().
For example, if LEVELS is 2, then FIELD(LEVELS,1,2,3,4)
returns 2 because 2 is the second argument to FIELD (LEVEL is not evaluated here).
Then, ELT(2, 'Level One', 'Level Two', 'Level Three', 'Level Four')
we return Level Two, which is the second argument to ELT() (don't count FIELD() as having an argument here).
In addition, if there is no matching LEVELS value in the list, IFNULL returns the default USER_LEVEL. In this way, we can combine ELT and FIELD to simulate Oracle's decode() function in MySQL.
Keep in mind that this solution may not be a good choice in terms of performance and readability, but it is a good option to explore MySQL's string functions.
Another point worth noting is that FIELD()
the output is not case sensitive. This means that both FIELD('B','B') and FIELD('b','B') return the same result, which is 1.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Changing max_allowed_packet Size in MySQL Server
Publish Date:2025/04/22 Views:192 Category:MySQL
-
This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi
Zerofill usage, advantages and alternatives in MySQL
Publish Date:2025/04/22 Views:195 Category:MySQL
-
In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input
Compare only MySQL timestamp dates to date parameters
Publish Date:2025/04/22 Views:64 Category:MySQL
-
In this article we will use the DATE() , CAST() , and CONVERT() functions to compare MySQL timestamp dates with only the date parameter. DATE() vs. CAST() vs. CONVERT() in MySQL Below is a brief description of each function. You can also fi
Calculating Percentages in MySQL
Publish Date:2025/04/22 Views:66 Category:MySQL
-
We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re
Selecting multiple values using WHERE in MySQL
Publish Date:2025/04/22 Views:185 Category:MySQL
-
This article is about using MySQL query to get data from a specific table or relation that satisfies a specific condition. To do this, the WHERE clause is used in the SQL query. WHERE clause in SQL query WHERE The clause specifies the condi
Changing the connection timeout in MySQL
Publish Date:2025/04/22 Views:59 Category:MySQL
-
We are learning how to change the connection timeout in MySQL using Linux (Ubuntu 20.04) and Windows operating systems. Changing the connection timeout in MySQL Sometimes you keep losing connection to the MySQL server because the connect_ti
MySQL fix Data Is Truncated for a Column error
Publish Date:2025/04/22 Views:101 Category:MySQL
-
This article describes possible causes and solutions for the MySQL error Data is truncated for a column . Fix data truncated due to column error in MySQL Here, we will discuss the possible causes and solutions to eliminate MySQL data trunca
MySQL Error Server PID File Could Not Be Found Solution
Publish Date:2025/04/22 Views:192 Category:MySQL
-
In this article, we will study about the Error! Error Server PID File Could Not Be Found! in MySQL and its solution with full explanation. MySQL PID file The file that contains the process identification number or process ID of a running My
Get the last inserted ID using PHP MySQLi function
Publish Date:2025/04/22 Views:99 Category:MySQL
-
This article briefly introduces the PHP mysqli() function and demonstrates how to use it to get the last inserted ID from a MySQL database. PHP mysqli() Function It is an extended version of the MySQL driver called mysqli and is typically u