{"id":21333,"date":"2021-11-28T13:35:00","date_gmt":"2021-11-28T03:35:00","guid":{"rendered":"https:\/\/database.guide\/?p=21333"},"modified":"2021-11-28T13:35:00","modified_gmt":"2021-11-28T03:35:00","slug":"sql-lpad","status":"publish","type":"post","link":"https:\/\/database.guide\/sql-lpad\/","title":{"rendered":"SQL LPAD()"},"content":{"rendered":"\n<p>In SQL, <code>LPAD()<\/code>is a commonly used function that pads the left part of a string with a specified character. The function can be used on strings and numbers, although depending on the <a href=\"https:\/\/database.guide\/what-is-a-dbms\/\" data-type=\"post\" data-id=\"799\">DBMS<\/a>, numbers may have to be passed as a string before they can be padded.<\/p>\n\n\n\n<p>DBMSs that have an <code>LPAD()<\/code> function include MySQL, MariaDB, PostgreSQL, and Oracle.<\/p>\n\n\n\n<p>DBMSs that <em>don&#8217;t<\/em> have an <code>LPAD()<\/code> function include SQL Server and SQLite (although there are other ways to apply left padding in these DBMSs). <\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s an example to demonstrate how to use <code>LPAD()<\/code> in your SQL code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT LPAD('Look Left', 20);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-----------------------+\n| LPAD('Look Left', 20) |\n+-----------------------+\n|            Look Left  |\n+-----------------------+<\/pre>\n\n\n\n<p>In this example, the left part of the string is padded with a space (the default padding character), and the resulting string is 20 characters long (because I specified <code>20<\/code> as the second argument).<\/p>\n\n\n\n<p>Oracle works the same, but we need to use <code>FROM DUAL<\/code> when doing a query like this (without querying an actual table):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT LPAD('Look Left', 20) \nFROM DUAL;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    LPAD('LOOKLEFT',20) \n_______________________ \n           Look Left   <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Specify a Padding Character<\/h2>\n\n\n\n<p>The padding doesn&#8217;t necessarily need to be a space. We can optionally add a third argument to specify the character (or characters) to use in the padding.<\/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 a number with zeros. Actually, I passed the number as a string in this example.<\/p>\n\n\n\n<p>In some DBMSs (such as MariaDB and MySQL) we can pass the number as a number, as well as the number to pad it with:<\/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>We can also do this in Oracle:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT LPAD(7, 3, 0) \nFROM DUAL;<\/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>But PostgreSQL has a problem with this:<\/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\">ERROR:  function lpad(integer, integer, integer) does not exist<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">SQL Server<\/h2>\n\n\n\n<p>SQL Server doesn&#8217;t have an <code>LPAD()<\/code> function, but it does have a <code><a href=\"https:\/\/database.guide\/how-the-format-function-works-in-sql-server-t-sql\/\" data-type=\"post\" data-id=\"3957\">FORMAT()<\/a><\/code> function that allows us to pad numbers with leading zeros:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT FORMAT(7, '000');<\/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>The way it works is that we pass the number, followed by a format string. In the above example, the format string is <code>000<\/code>. This is a <a href=\"https:\/\/database.guide\/custom-numeric-format-strings-supported-by-format-in-sql-server\/\" data-type=\"post\" data-id=\"4629\">custom numeric format string<\/a> that results in the original number being padded so that the result consists of three digits. If there aren&#8217;t already three digits in the original number, then it&#8217;s padded with zeros.<\/p>\n\n\n\n<p>To left pad strings in SQL Server, we can do something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT RIGHT('.......' + 'Cat', 7);<\/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>There are also a couple of other <a href=\"https:\/\/database.guide\/left-padding-in-sql-server-3-lpad-equivalents\/\" data-type=\"post\" data-id=\"2002\">LPAD() equivalents in SQL Server.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In SQL, LPAD()is a commonly used function that pads the left part of a string with a specified character. The function can be used on strings and numbers, although depending on the DBMS, numbers may have to be passed as a string before they can be padded. DBMSs that have an LPAD() function include MySQL, &#8230; <a title=\"SQL LPAD()\" class=\"read-more\" href=\"https:\/\/database.guide\/sql-lpad\/\" aria-label=\"Read more about SQL LPAD()\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117,6,41,40,7,5],"tags":[93,67,20],"class_list":["post-21333","post","type-post","status-publish","format-standard","hentry","category-mariadb","category-mysql","category-oracle","category-postgresql","category-sql","category-sql-server","tag-functions","tag-string-functions","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/21333","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=21333"}],"version-history":[{"count":6,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/21333\/revisions"}],"predecessor-version":[{"id":21380,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/21333\/revisions\/21380"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=21333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=21333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=21333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}