EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL DECODE()
Secondary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions

PostgreSQL DECODE()

By Sohel SayyadSohel Sayyad

PostgreSQL DECODE()

Definition of PostgreSQL DECODE() Function

PostgreSQL  DECODE() function is used to decode or extract the binary data from the input string, which is in textual format and which has been encoded by using PostgreSQL Encode() function. The PostgreSQL  DECODE() function takes input text which needs to be decoded and a parameter type in which the user wants it to decode. The parameter given to the PostgreSQL Decode() function should be the same as the type of the parameter used in the case of the PostgreSQL Encode() function. The PostgreSQL supports various formats for Encode and DECODE functions such as base64, escape, etc.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Consider the following syntax in order to illustrate the PostgreSQL DECODE() function:

decode(string input_text, format type_text)

Explanation:

  • input_text – This defines the input text string, which will be decoded.
  • type_text – This defines the in which the input text we expect to be decoded.

How does PostgreSQL DECODE() Function Works?

  1. The PostgreSQL DECODE() function takes two input parameters; first is the text which the user wants to decode, and seconds is the parameter type in which the user wants it to decode.
  2. The parameter given to the PostgreSQL Decode() function should be the same as the type of the parameter used in the case of the PostgreSQL Encode() function.
  3. The PostgreSQL  DECODE() function returns us the text’s decoded binary data based on the specified type.

Examples

  1. Consider the following example where we will encode a string ‘encode base64 string’ with format ‘base64’ by using the following SQL statement.

select encode('encode base64 string', 'base64');

Illustrate the result of the above statement by using the following snapshot.

Data Input

We will use the encoded ‘ZW5jb2RlIGJhc2U2NCBzdHJpbmc=’ from the above encode example and use it for decode by using the following SQL statement.

select decode('ZW5jb2RlIGJhc2U2NCBzdHJpbmc=', 'base64');

Illustrate the result of the above statement by using the following snapshot.

PostgreSQL DECODE() 1

  1. Consider the following example where we will encode a string ‘encode escape string’ with format ‘escape’ by using the following SQL statement.

select encode('encode escape string', 'escape');

Illustrate the result of the above statement by using the following snapshot.

PostgreSQL DECODE() 2

We will then use the encoded ‘encode escape string’ from the above encode example and use it for decode by using the following SQL statement.

select decode('encode escape string', 'escape');

PostgreSQL DECODE() 3

Illustrate the result of the above statement by using the following snapshot.

  1. Consider the following example where we will create a new table by using the CREATE TABLE statement, which will store the details of the tuitions.

CREATE TABLE tuitions (
id serial PRIMARY KEY,
tuition_name VARCHAR NOT NULL,
tuition_start_tm TIME NOT NULL,
tuition_end_tm TIME NOT NULL
);

Now we will encode the name of the tuitions using base64 format and then will insert some data into the tuitions table by using the INSERT INTO statement as follow:

INSERT INTO tuitions(tuition_name, tuition_start_tm, tuition_end_tm)
VALUES
( encode('Maths','base64'), '07:00:00', '08:45:00'),
( encode('English','base64'), '09:00:00', '11:45:00'),
( encode('Algebra','base64'), '12:15:00', '14:00:00'),
( encode('CS','base64'), '14:15:00', '17:00:00'),
( encode('History','base64'), '17:15:00', '19:00:00');

Illustrate the result of the tuitions table by using the following SQL statement and a snapshot.

SELECT * FROM tuitions;

Data Output

Consider the following example of the PostgreSQL DECODE() function, which will return us the binary data from the tuition_name column, here we will be using the ‘base64’ as a format type because we have used the same while inserting data into the table.

select id, decode(tuition_name, 'base64'), tuition_start_tm, tuition_end_tm from tuitions;

Illustrate the result of the above statement by using the following snapshot.

Data Output 1

Here in the above state of the above table, all rows were having encoded records for the tuition_name column. Now we will insert some records in the ‘tuitions’ table without encoding the tuition_name by using the following INSERT INTO statement.

INSERT INTO tuitions(tuition_name, tuition_start_tm, tuition_end_tm)
VALUES('Maths', '07:00:00', '08:45:00'),
('English', '09:00:00', '11:45:00'),
('Algebra', '12:15:00', '14:00:00'),
('CS', '14:15:00', '17:00:00'),
('History', '17:15:00', '19:00:00');

Illustrate the result of the tuitions table by using the following SQL statement and a snapshot.

SELECT * FROM tuitions;

PostgreSQL DECODE() 4

Now, we have mixed encoded and decoded data in the ‘tuitions’ table, so whenever we try to retrieve the records by decoding the ‘tuition_name’, we will get the exception.

Illustrate the expected exception by using the following SQL statement and snapshots:

select id, decode(tuition_name, 'base64'), tuition_start_tm, tuition_end_tm from tuitions;

PostgreSQL DECODE() 5

Conclusion

We hope from the above article you have understood how to use the PostgreSQL DECODE() function and how the PostgreSQL DECODE() function works. Also, we have added several examples of the PostgreSQL DECODE() function to understand it in detail.

Recommended Articles

This is a guide to PostgreSQL DECODE(). Here we discuss the Introduction to PostgreSQL DECODE(), Syntax, working, and respective examples. You can also go through our other related articles to learn more –

  1. PostgreSQL NOT NULL
  2. PostgreSQL Timestamp
  3. Foreign Key in PostgreSQL
  4. PostgreSQL Subquery
Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |  7+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more