Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in SQL by (6.1k points)

I am attempting to get an equivalent of DECODE function in MySQL. It operates 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 the value of the column 'Age' with 13, 14, 15.. and return the appropriate string value 'Thirteen', 'Fourteen'.. and if it matches with nothing, then default value of 'Adult' will be returned.

Any ideas which function in the MySQL can do this same work?

CLARIFICATION: I agree using CASE is one way of achieving the desired result, however I am rather looking for a function because of performance and other reasons.

1 Answer

0 votes
by (12.7k points)

For MySQL, You can use IF() wherein the oracle you would have used DECODE().

mysql> select if(emp_id=1,'X','Y') as test, emp_id from emps; 

Want to be a SQL expert? You can join the Intellipaat's SQL Certification program to learn more.

For more details on MySQL, refer to the below MySQL Tutorial video. 

Related questions

0 votes
1 answer
asked Dec 16, 2020 in SQL by Appu (6.1k points)
0 votes
2 answers
0 votes
3 answers
asked Dec 29, 2020 in SQL by Appu (6.1k points)

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...