I am trying to find an equivalent of DECODE function in MySQL. It works like this:
Select Name, DECODE(Age,
13,'Thirteen',14,'Fourteen',15,'Fifteen',16,'Sixteen',
17,'Seventeen',18,'Eighteen',19,'Nineteen',
'Adult') AS AgeBracket
FROM Person
The DECODE function will compare value of column 'Age' with 13, 14, 15.. and return appropriate string value 'Thirteen', 'Fourteen'.. and if it matches with nothing, then default value of 'Adult' will be returned.
Any ideas which function in MySQL can do this job? Thanks.
CLARIFICATION: I agree using CASE is one way of achieving desired result, but I am rather looking for a function because of performance and other reasons.
The MySQL DECODE() function is used for decoding an encoded string and return the original string. The MySQL DECODE() function returns empty strings if the encoded string is an empty string.
DECODE compares expr to each search value one by one. If expr is equal to a search , then Oracle Database returns the corresponding result . If no match is found, then Oracle returns default . If default is omitted, then Oracle returns null.
CASE is a Statement in SQL / PL SQL. DECODE is a function in Oracle that can be used in SQL only. DECODE can work as a function inside SQL only. CASE can be an efficient substitute for IF-THEN-ELSE in PL/SQL.
From performance perspective, In Oracle decode and CASE does not make any difference. But in Exadata , Decode is faster than CASE. The Decode operation is done at storage Server level where the data is present BUT CASE is done at DB Instance level which receives data from DB storage Level.
You can use IF()
where in Oracle you would have used DECODE()
.
mysql> select if(emp_id=1,'X','Y') as test, emp_id from emps;
Select Name,
case
when Age = 13 then 'Thirteen'
when Age = 14 then 'Fourteen'
when Age = 15 then 'Fifteen'
when Age = 16 then 'Sixteen'
when Age = 17 then 'Seventeen'
when Age = 18 then 'Eighteen'
when Age = 19 then 'Nineteen'
else 'Adult'
end as AgeBracket
FROM Person
You can use a CASE statement...however why don't you just create a table with an integer for ages between 0 and 150, a varchar for the written out age and then you can just join on that
Another MySQL option that may look more like Oracle's DECODE
is a combination of FIELD
and ELT
. In the code that follows, FIELD()
returns the argument list position of the string that matches Age. ELT()
returns the string from ELT
s argument list at the position provided by FIELD()
. For example, if Age
is 14
, FIELD(Age, ...)
returns 2
because 14
is the 2nd argument of FIELD
(not counting Age
). Then, ELT(2, ...)
returns 'Fourteen'
, which is the 2nd argument of ELT
(not counting the FIELD()
argument). IFNULL
returns the default AgeBracket
if no match to Age
is found in the list.
Select Name, IFNULL(ELT(FIELD(Age,
13, 14, 15, 16, 17, 18, 19),'Thirteen','Fourteen','Fifteen','Sixteen',
'Seventeen','Eighteen','Nineteen'),
'Adult') AS AgeBracket
FROM Person
While I don't think this is the best solution to the question either in terms of performance or readability it is interesting as an exploration of MySQL's string functions. Keep in mind that FIELD
's output does not seem to be case sensitive. I.e., FIELD('A','A')
and FIELD('a','A')
both return 1
.
The example translates directly to:
Select Name, CASE Age
WHEN 13 then 'Thirteen' WHEN 14 then 'Fourteen' WHEN 15 then 'Fifteen' WHEN 16 then 'Sixteen'
WHEN 17 then 'Seventeen' WHEN 18 then 'Eighteen' WHEN 19 then 'Nineteen'
ELSE 'Adult' END AS AgeBracket
FROM Person
which you may prefer to format e.g. like this:
Select Name,
CASE Age
when 13 then 'Thirteen'
when 14 then 'Fourteen'
when 15 then 'Fifteen'
when 16 then 'Sixteen'
when 17 then 'Seventeen'
when 18 then 'Eighteen'
when 19 then 'Nineteen'
else 'Adult'
END AS AgeBracket
FROM Person
If additional table doesn't fit, you can write your own function for translation.
The plus of sql function over case is, that you can use it in various places, and keep translation logic in one place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With