Art of BI: SQL Pad Left Functionality (Oracle / SQL Server)

By | In Art of BI, Big Data, Business Intelligence | February 11th, 2010

Every now an then one needs to pad a column value with a character or symbol in order to conform the resulting value to some standard. For example, a company may represent their stores by a maximum of 4 digits and each character in that 4-digit store number must contain a value as to not be confused with any other values..  In other words, store #99 would be represented as 0099 instead of just 99.

This is not uncommon practice. Often times, similar functionality is required when pulling data from the mainframe where “super keys” are paramount.  Our good friends at MS have set up SQL Server so that this is universally simple.  The code snippet in its basic form looks like this:
[sourcecode language=’sql’]
RIGHT(‘0000’ +  [COLUMN NAME], 4)
[/sourcecode]

This is straightforward.  Use the RIGHT function by concatenating the first parameter to a set number of padding characters and the column name in your table. Be sure to CAST that column as a string data type if pulling a numeric field.  Then, for the RIGHT functions second parameter, make its value the same number of character spaces required for your final value.

[simage=101,400,y,center]

Oracle just had to be difficult.  In order to conduct the same operation using PL/SQL you must use a function, which I am still not sure how it completely works behind the scenes.  The syntax is as follows:
[sourcecode language=’sql’]
SELECT to_char([COLUMN NAME],’FM0000′) AS result FROM table;
[/sourcecode]

Update
Recently a reader posted the use of the Oracle function LPAD, which is much cleaner than the use of the FM function and works better than the SQL Server function. The syntax would be
[sourcecode language=’sql’]
SELECT LPAD(‘Test’, 8, ‘0’) AS result FROM dual;
[/sourcecode]

This LPAD function has been relevant since 8i.

Conclusion

Each of the code snippets above will provide a conformed look at a column value.  SQL Server’s method seems to be a little more cut n’ dry than PL/SQL’s but they both get the job done.

Another great source for Oracle functions can be found at http://www.techonthenet.com/oracle/functions/lpad.php

Contact Us
Christian Screen
Christian is an innovator in analytics and data warehousing design, best practices, and delivery. With more than fifteenyears of decision support and data warehousing with key experiences at Office Depot HQ, Sierra-Cedar, and Capgemini, he oversees the Oracle Analytics Practice which includes the technical development and delivery of Oracle BI collaboration software, data warehouse solutions, Oracle BI/EPM projects, and packaged analytics solutions at Datavail.

Leave a Reply

Your email address will not be published.
Required fields are marked (*).

1 thought on “Art of BI: SQL Pad Left Functionality (Oracle / SQL Server)”
  1. Oracle has a couple of options.

    The simplest is LPAD(column,4,'0'). The column is converted into a string (if it isn't already) then left padded to a length of four with the '0' character. If the '0' is omitted, it pads with spaces.

    That TO_CHAR is explictly for converting a number to a character. The '0' mask means show leading/trailing zeros. A money mask might be '9990.00' which would show at least one digit in the 'dollar' position and two in the cents positions. A negative value would still have a minus sign, and would show as -0005 (for example). Using lpad you would get 00-5, so before you decide what to use it is important to consider how you want display negative, decimal and numbers larger than the ones that fit the format mask

    The FM component means suppress the space that would otherwise be there for a positive value.