{"id":1838,"date":"2025-02-02T21:07:51","date_gmt":"2025-02-02T14:07:51","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=1838"},"modified":"2025-02-02T21:08:00","modified_gmt":"2025-02-02T14:08:00","slug":"postgresql-format","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-format\/","title":{"rendered":"PostgreSQL FORMAT Function"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the PostgreSQL <code>FORMAT()<\/code> function to construct formatted messages and dynamic SQL statements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-postgresql-format-function'>Introduction to the PostgreSQL FORMAT function <a href=\"#introduction-to-the-postgresql-format-function\" class=\"anchor\" id=\"introduction-to-the-postgresql-format-function\" title=\"Anchor for Introduction to the PostgreSQL FORMAT function\">#<\/a><\/h2>\n\n\n\n<p>The <code>FORMAT()<\/code> function return a formatted string based a template.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>FORMAT()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">FORMAT(format_string, format_argument1, format_argument2, ...)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>FORMAT()<\/code> function accepts a variable number of parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>format_string<\/code>: This is the string that includes <em>format specifiers<\/em>.<\/li>\n\n\n\n<li><code>format_argument1<\/code>, <code>format_argument2<\/code>, &#8230;: the arguments you use to in the <code>format_string<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>The <code>format_string<\/code> may include one or more format specifiers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>%s<\/code> formats the argument as a string.<\/li>\n\n\n\n<li><code>%I<\/code> formats the argument as an SQL identifier, such as a table name with proper quoting.<\/li>\n\n\n\n<li><code>%L<\/code> formats the argument as an SQL literal.<\/li>\n<\/ul>\n\n\n\n<p>To include the percent sign <code>(%<\/code>), you can use two percent sign <code>%%<\/code>. The first one escapes the second.<\/p>\n\n\n\n<p>The format specifiers <code>%I<\/code> and <code>%L<\/code> can be handy for constructing dynamic SQL statements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='basic-postgresql-format-function-examples'>Basic PostgreSQL FORMAT function examples <a href=\"#basic-postgresql-format-function-examples\" class=\"anchor\" id=\"basic-postgresql-format-function-examples\" title=\"Anchor for Basic PostgreSQL FORMAT function examples\">#<\/a><\/h2>\n\n\n\n<p>The following <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-select\/\">SELECT<\/a><\/code> statement uses the <code>FORMAT<\/code> function to return a formatted string:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  FORMAT(<span class=\"hljs-string\">'Hello, %s'<\/span>, <span class=\"hljs-string\">'pgtutorial.com'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIEZPUk1BVCgnSGVsbG8sICVzJywgJ3BndHV0b3JpYWwuY29tJyk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">        <span class=\"hljs-keyword\">format<\/span>\n<span class=\"hljs-comment\">-----------------------<\/span>\n Hello, pgtutorial.com<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>FORMAT<\/code> function replaces the format specifier <code>%s<\/code> by the string argument <code>'pgtutorial.com'<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='constructing-dynamic-sql-statements'>Constructing dynamic SQL statements <a href=\"#constructing-dynamic-sql-statements\" class=\"anchor\" id=\"constructing-dynamic-sql-statements\" title=\"Anchor for Constructing dynamic SQL statements\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the <code>FORMAT<\/code> function with the <code>%I<\/code> specifiers to return a dynamic SQL statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  FORMAT(\n    <span class=\"hljs-string\">'SELECT %I, %I FROM %I'<\/span>,\n    <span class=\"hljs-string\">'product_name'<\/span>,\n    <span class=\"hljs-string\">'price'<\/span>,\n    <span class=\"hljs-string\">'products'<\/span>\n  ) sql_statement;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIEZPUk1BVCggJ1NFTEVDVCAlSSwgJUkgRlJPTSAlSScsICdwcm9kdWN0X25hbWUnLCAncHJpY2UnLCAncHJvZHVjdHMnICkgc3FsX3N0YXRlbWVudDs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">              sql_statement\n<span class=\"hljs-comment\">------------------------------------------<\/span>\n <span class=\"hljs-keyword\">SELECT<\/span> product_name, price <span class=\"hljs-keyword\">FROM<\/span> products<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following query uses the <code>FORMAT<\/code> function with the <code>%L<\/code> for formatting literals:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  FORMAT(\n    <span class=\"hljs-string\">'SELECT %I, %I FROM %I WHERE %I = %L'<\/span>,\n    <span class=\"hljs-string\">'product_name'<\/span>,\n    <span class=\"hljs-string\">'price'<\/span>,\n    <span class=\"hljs-string\">'products'<\/span>,\n    <span class=\"hljs-string\">'product_name'<\/span>,\n    <span class=\"hljs-string\">'Apple iPhone 15'<\/span>\n  ) sql_statement;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIEZPUk1BVCggJ1NFTEVDVCAlSSwgJUkgRlJPTSAlSSBXSEVSRSAlSSA9ICVMJywgJ3Byb2R1Y3RfbmFtZScsICdwcmljZScsICdwcm9kdWN0cycsICdwcm9kdWN0X25hbWUnLCAnQXBwbGUgaVBob25lIDE1JyApIHNxbF9zdGF0ZW1lbnQ7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">                                  sql_statement\n<span class=\"hljs-comment\">---------------------------------------------------------------------------------<\/span>\n <span class=\"hljs-keyword\">SELECT<\/span> product_name, price <span class=\"hljs-keyword\">FROM<\/span> products <span class=\"hljs-keyword\">WHERE<\/span> product_name = <span class=\"hljs-string\">'Apple iPhone 15'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>%L<\/code> format specifier quotes the literal string correctly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='practical-examples-of-the-format-function'>Practical examples of the FORMAT function <a href=\"#practical-examples-of-the-format-function\" class=\"anchor\" id=\"practical-examples-of-the-format-function\" title=\"Anchor for Practical examples of the FORMAT function\">#<\/a><\/h2>\n\n\n\n<p>First, <a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-functions\/\">create a PL\/pgSQL function<\/a> that uses the <code>FORMAT()<\/code> function to select all columns from the <code>products<\/code> table and sort the result set based on a column in an order specified by parameters:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">OR REPLACE<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> get_products_sorted (\n  p_sort_column <span class=\"hljs-type\">TEXT<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-string\">'product_id'<\/span>,\n  p_sort_order <span class=\"hljs-type\">TEXT<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-string\">'ASC'<\/span>\n) <span class=\"hljs-keyword\">RETURNS<\/span> <span class=\"hljs-keyword\">SETOF<\/span> products <span class=\"hljs-keyword\">AS<\/span> $$<span class=\"pgsql\">\n<span class=\"hljs-keyword\">DECLARE<\/span>\n\u00a0 \u00a0\u00a0sql_command <span class=\"hljs-type\">TEXT<\/span>;\n\u00a0 \u00a0\u00a0sort_order_clean <span class=\"hljs-type\">TEXT<\/span>;\n<span class=\"hljs-keyword\">BEGIN<\/span>\n\u00a0 \u00a0\u00a0<span class=\"hljs-keyword\">IF<\/span> UPPER(p_sort_order) = <span class=\"hljs-string\">'DESC'<\/span> <span class=\"hljs-keyword\">THEN<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0sort_order_clean := <span class=\"hljs-string\">'DESC'<\/span>;\n\u00a0 \u00a0\u00a0<span class=\"hljs-keyword\">ELSE<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0sort_order_clean := <span class=\"hljs-string\">'ASC'<\/span>;\n\u00a0 \u00a0\u00a0<span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;\n\n\u00a0 \u00a0\u00a0<span class=\"hljs-comment\">-- Construct the dynamic SELECT statement.<\/span>\n\u00a0 \u00a0\u00a0sql_command := format(<span class=\"hljs-string\">'SELECT * FROM products ORDER BY %I %s;'<\/span>,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0p_sort_column,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0sort_order_clean);\n\n\u00a0 \u00a0\u00a0<span class=\"hljs-comment\">-- Optionally, log the dynamic SQL for debugging purposes.<\/span>\n\u00a0 \u00a0\u00a0<span class=\"hljs-keyword\">RAISE<\/span> <span class=\"hljs-keyword\">NOTICE<\/span> <span class=\"hljs-string\">'Executing SQL: %'<\/span>, sql_command;\n\n\u00a0 \u00a0\u00a0<span class=\"hljs-comment\">-- Execute the dynamic SQL and return the result.<\/span>\n\u00a0 \u00a0\u00a0<span class=\"hljs-keyword\">RETURN QUERY<\/span> <span class=\"hljs-keyword\">EXECUTE<\/span> sql_command;\n<span class=\"hljs-keyword\">END<\/span>;\n$$<\/span> <span class=\"hljs-keyword\">LANGUAGE<\/span> plpgsql;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, retrieve the product name and price, and sort the products by the price from high to low by calling the <code>get_products_sorted<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  product_name,\n  price\n<span class=\"hljs-keyword\">FROM<\/span>\n  get_products_sorted (<span class=\"hljs-string\">'price'<\/span>, <span class=\"hljs-string\">'DESC'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIHByb2R1Y3RfbmFtZSwgcHJpY2UgRlJPTSBnZXRfcHJvZHVjdHNfc29ydGVkICgncHJpY2UnLCAnREVTQycpOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\">        product_name        |  price\n<span class=\"hljs-comment\">----------------------------+---------<\/span>\n Samsung QN900C Neo QLED    | <span class=\"hljs-number\">2999.99<\/span>\n LG G3 OLED                 | <span class=\"hljs-number\">2499.99<\/span>\n Sony Bravia XR A95K        | <span class=\"hljs-number\">2499.99<\/span>\n LG OLED TV C3              | <span class=\"hljs-number\">1999.99<\/span>\n Samsung Galaxy Z Fold <span class=\"hljs-number\">5<\/span>    | <span class=\"hljs-number\">1799.99<\/span>\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>FORMAT<\/code> function to construct a formatted string or a dynamic SQL statement.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1838\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-format\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL FORMAT Function\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1838\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-format\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL FORMAT Function\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the PostgreSQL FORMAT() function to construct formatted messages and dynamic SQL statements.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1800,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1838","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL FORMAT Function<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL FORMAT function to construct formatted messages and dynamic SQL statements.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-format\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL FORMAT Function\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL FORMAT function to construct formatted messages and dynamic SQL statements.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-format\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-02T14:08:00+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-string-functions\\\/postgresql-format\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-string-functions\\\/postgresql-format\\\/\",\"name\":\"PostgreSQL FORMAT Function\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2025-02-02T14:07:51+00:00\",\"dateModified\":\"2025-02-02T14:08:00+00:00\",\"description\":\"In this tutorial, you'll learn how to use the PostgreSQL FORMAT function to construct formatted messages and dynamic SQL statements.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-string-functions\\\/postgresql-format\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-string-functions\\\/postgresql-format\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-string-functions\\\/postgresql-format\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL String Functions\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-string-functions\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL FORMAT Function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PostgreSQL FORMAT Function","description":"In this tutorial, you'll learn how to use the PostgreSQL FORMAT function to construct formatted messages and dynamic SQL statements.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-format\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL FORMAT Function","og_description":"In this tutorial, you'll learn how to use the PostgreSQL FORMAT function to construct formatted messages and dynamic SQL statements.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-format\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-02-02T14:08:00+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-format\/","url":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-format\/","name":"PostgreSQL FORMAT Function","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2025-02-02T14:07:51+00:00","dateModified":"2025-02-02T14:08:00+00:00","description":"In this tutorial, you'll learn how to use the PostgreSQL FORMAT function to construct formatted messages and dynamic SQL statements.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-format\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-format\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/postgresql-format\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL String Functions","item":"https:\/\/www.pgtutorial.com\/postgresql-string-functions\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL FORMAT Function"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1838","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=1838"}],"version-history":[{"count":4,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1838\/revisions"}],"predecessor-version":[{"id":1842,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1838\/revisions\/1842"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1800"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=1838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}