{"id":21299,"date":"2021-10-30T10:14:39","date_gmt":"2021-10-30T00:14:39","guid":{"rendered":"https:\/\/database.guide\/?p=21299"},"modified":"2021-10-30T10:14:39","modified_gmt":"2021-10-30T00:14:39","slug":"lpad-function-in-postgresql","status":"publish","type":"post","link":"https:\/\/database.guide\/lpad-function-in-postgresql\/","title":{"rendered":"LPAD() Function in PostgreSQL"},"content":{"rendered":"\n<p>In PostgreSQL, <code>LPAD()<\/code>is a function that enables us to add padding to the left part of a string.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The syntax goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lpad ( string text, length integer &#91;, fill text ] ) <\/code><\/pre>\n\n\n\n<p>Where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code><em>string text<\/em><\/code> is the string to apply padding to<\/li><li><em><code>length integer<\/code><\/em> is the total length that you want the string to be after padding has been applied<\/li><li><em><code>fill text<\/code><\/em> is an optional argument that allows you to specify one or more characters to use as the padding (the default is a space).<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s an example to demonstrate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT LPAD('7', 3, '0');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">007<\/pre>\n\n\n\n<p>In this case, I padded the string with zeros.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Padding with Spaces<\/h2>\n\n\n\n<p>As mentioned, the last argument is optional, and the default padding character is a space.<\/p>\n\n\n\n<p>Here&#8217;s an example of padding a string with spaces:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT LPAD('Cat', 10);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">        Cat<\/pre>\n\n\n\n<p>We can see that the string has been padded with spaces to its left.<\/p>\n\n\n\n<p>This is the equivalent of doing the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT LPAD('Cat', 10, ' ');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">        Cat<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Padding with Multiple Characters<\/h2>\n\n\n\n<p>The third argument can contain more than one character:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT LPAD('Cat', 10, 'Dog');<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">DogDogDCat<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In PostgreSQL, LPAD()is a function that enables us to add padding to the left part of a string.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[93,67,20],"class_list":["post-21299","post","type-post","status-publish","format-standard","hentry","category-postgresql","tag-functions","tag-string-functions","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/21299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=21299"}],"version-history":[{"count":2,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/21299\/revisions"}],"predecessor-version":[{"id":21305,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/21299\/revisions\/21305"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=21299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=21299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=21299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}