PostgreSQL - FORMAT Function
Last Updated :
14 Oct, 2024
The PostgreSQL format()
function is a powerful tool for string formatting by allowing developers to insert variables into strings using format specifiers like %s
, %I
, and %L
. This function is especially useful for building dynamic SQL queries and ensuring proper formatting of identifiers.
It simplifies complex string manipulations and enhances security by preventing SQL injection. In this article, We will learn about the FORMAT Function in PostgreSQL in detail by understanding various aspects.
PostgreSQL Format()
Function
- The
format()
function in PostgreSQL is used to format strings in a way similar to the printf()
function in C.
- It allows placeholders (like
%s
, %I
, or %L
) to be replaced with variable values.
- This can be particularly useful in dynamically generating SQL statements, user-friendly messages or customized data outputs in queries.
Syntax
format(format_string [, format_arg [, ... ]])
Explanation:
- format_string: The string containing the format specifiers.
- format_arg: The arguments to replace the format specifiers.
Supported Format Specifiers
The format()
function supports various format specifiers to handle different data types:
Specifier | Description |
---|
%s | Replaces with a string. |
%I | Replaces with an identifier (table or column name). |
%L | Replaces with a literal (safely quoted). |
%t | Replaces with a Boolean. |
%D | Replaces with a numeric, double-precision number. |
Examples of Using the format()
Function
Let us take a look at some of the examples of FORMAT Function in PostgreSQL to better understand the concept.
Example 1: Simple String Formatting
The following statement uses the FORMAT() function to format a string:
SELECT FORMAT('Hello, %s', 'Geeks!!');
Output:

Example 2: Constructing Customer Full Names
The following statement uses the FORMAT() function to construct customer' full names from first names and last names from the customers table of the sample database which is dvdrental:
SELECT
FORMAT('%s, %s', last_name, first_name) full_name
FROM
customer;
ORDER BY
full_name;
Output:

Important Points About FORMAT Function in PostgreSQL
- The
FORMAT()
function is variadic, meaning it can accept a varying number of arguments.
- The '
I'
and 'L'
type specifiers allow the FORMAT()
function to format SQL identifiers and literals.
- The '
-
' flag left-justifies the output, while the '0
'
flag pads the output with zeros instead of spaces.
- The
FORMAT()
function allows the use of positional parameters with the syntax '%n$
'
, where n
is the position of the argument in the list.
Conclusion
The PostgreSQL format()
function is a versatile tool for formatting strings, making it an excellent choice for dynamic SQL generation and improving code readability. Whether you’re dealing with simple text or complex identifiers and literals, the format()
function provides a robust way to customize your outputs and queries.
Similar Reads
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Window Functions in SQL SQL window functions are essential for advanced data analysis and database management. It is a type of function that allows us to perform calculations across a specific set of rows related to the current row. These calculations happen within a defined window of data and they are particularly useful
6 min read
Top 60 DBMS Interview Questions with Answers for 2025 A Database Management System (DBMS) is the backbone of modern data storage and management. Understanding DBMS concepts is critical for anyone looking to work with databases. Whether you're preparing for your first job in database management or advancing in your career, being well-prepared for a DBMS
15+ min read
SQL | WITH Clause SQL queries can sometimes be complex, especially when you need to deal with multiple nested subqueries, aggregations, and joins. This is where the SQL WITH clause also known as Common Table Expressions (CTEs) comes in to make life easier. The WITH Clause is a powerful tool that simplifies complex SQ
6 min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced SQL (Structured Query Language) is a powerful and flexible tool for managing and manipulating relational databases. Regardless of our experience level, practising SQL exercises is essential for improving our skills. Regular practice not only enhances our understanding of SQL concepts but also builds
15+ min read
SQL Cheat Sheet ( Basic to Advanced) Creating and managing databases in SQL involves various commands and concepts that handle the structuring, querying, and manipulation of data. In this guide, we will see a comprehensive cheat sheet for essential SQL operations, offering a practical reference for tasks ranging from database creation
15 min read
SQL Views Views in SQL are a type of virtual table that simplifies how users interact with data across one or more tables. Unlike traditional tables, a view in SQL does not store data on disk; instead, it dynamically retrieves data based on a pre-defined query each time itâs accessed. SQL views are particular
7 min read
MySQL Tutorial This MySQL Tutorial is made for both beginners and experienced professionals. Whether you're starting with MYSQL basics or diving into advanced concepts, this free tutorial is the ideal guide to help you learn and understand MYSQL, no matter your skill level. From setting up your database to perform
11 min read