Skip to content
Related Articles

Related Articles

MySQL | DECODE( ) Function
  • Last Updated : 13 Nov, 2019

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('geeksforgeeks', 'passwordstring'), 'passwordstring'); 

Output:

geeksforgeeks 

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

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

Output:

geeksforgeeks123 

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 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :