MySQL | DECODE( ) Function

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.

The DECODE() function accepts two parameters which are the encoded string to be decoded and the password string to decode the encoded string.

Syntax:

DECODE(encoded_string, password_string);

Parameters Used:

  • encoded_string – It is used to specify the encoded string that is to be decoded.
  • password_string – It is used to specify the password string to decode the encoded string.

Return Value:
The DECODE function in MySQL returns the original string after decoding an encoded string.

Supported Versions of MySQL:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1

Example-1: Implementing DECODE function on a string.

SELECT  
DECODE(ENCODE('w3wiki', 'passwordstring'), 'passwordstring'); 

Output:

w3wiki 

Example-2: Implementing DECODE function on a string which has a combination of characters and integers.

SELECT  
DECODE(ENCODE('w3wiki123', 'passwordstring'), 'passwordstring'); 

Output:

w3wiki123 

Example-3: Implementing DECODE function on a NULL string and returning the length of the string after compression.

SELECT  
DECODE(ENCODE('NULL', 'passwordstring'), 'passwordstring'); 

Output:

NULL