MySQL DECODE() function
DECODE() function
MySQL DECODE() function decodes an encoded string and returns the original string.
Syntax:
DECODE(crypt_str, pass_str);
Arguments:
Name | Description |
---|---|
crypt_str | An encoded string. |
pass_str | The password string to decode crypt_str. |
Syntax Diagram:

MySQL Version: 5.6
Example:
Code:
SELECT DECODE(ENCODE('mytext','mykeystring'),'mykeystring');
Explanation:
The above MySQL statement decodes the encoded string 'mytext' as specified in the argument and returns the original string.
Sample Output:
mysql> SELECT DECODE(ENCODE('mytext','mykeystring'),'mykeystring'); +------------------------------------------------------+ | DECODE(ENCODE('mytext','mykeystring'),'mykeystring') | +------------------------------------------------------+ | mytext | +------------------------------------------------------+ 1 row in set (0.00 sec)
PHP script:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>example-decode - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Retrieving the decrypted data from encrypted 'description' column from 'testtable' : </h2>";
echo "<table border='1' style='border-collapse: collapse; border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Description</td><td width='100' align='center'>DECODE(description,'mypassw')</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT description,DECODE(description,'mypassw')
FROM testtable");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row[" description"] . "</td>";
echo "<td align='center' width='200'>" . $row[" DECODE(description,'mypassw')"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
Example : MySQL decode() function using table
Sample table: testtable
Code:
SELECT description, DECODE(description,'mypassw')
FROM testtable;
Explanation:
The above MySQL statement retrieves the decoded data from encoded 'description' column from 'testtable'.
Sample Output:
mysql> SELECT description, DECODE(description,'mypassw') -> FROM testtable; +---------------------------+-------------------------------+ | description | DECODE(description,'mypassw') | +---------------------------+-------------------------------+ | ^5[@·˜,IÜç¦Éý | zÝÀâ“x®,ÕCý’ | | Ô£^]Žþª_‹m | myencodetext | | ÿ»(õ 2ñ«QèªöjD¸=ËTú9Ž! | ›zוämù…w¤xÿ&‚ÛjV|3üKÜ | | ^5[@·˜,IÜç¦Éý | zÝÀâ“x®,ÕCý’ | +---------------------------+-------------------------------+ 4 rows in set (0.00 sec)
PHP script:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>example1-decode - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Retrieving the decrypted data from encrypted 'description' column from 'testtable' : </h2>";
echo "<table border='1' style='border-collapse: collapse; border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Description</td><td width='100' align='center'>DECODE(description,'mypassw')</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT description,DECODE(description,'mypassw')
FROM testtable");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row[" description"] . "</td>";
echo "<td align='center' width='200'>" . $row[" DECODE(description,'mypassw')"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
Online Practice Editor:
Previous: COMPRESS()
Next: DES_DECRYPT()
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook