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('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 
My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.