Home > Ask the Oracle Experts > SQL Questions & Answers > Padding with zeroes
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Padding with zeroes

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 10 February 2004
In MS Access 2000, I have to concatenate a number of text fields into one field, but the length must not vary. I am having problems to get the data to "pad" with zeroes. The fields that are being concatenated are text fields with numeric values. For example township is a three-character field, but the values vary from 1 to 999. Where it is only two characters, for example 45, it must be represented as 045 in the concatenated field. Can you help?

>
EXPERT RESPONSE

There are a number of tricks to this type of problem.

First, we need to handle nulls. If you concatenate some character string values together, and one of them happens to be null, then the entire concatenated result will be null. So we need COALESCE on each field being concatenated, to default to an empty string.

Second, we want a method which always works, no matter whether we are concatenating strings or bits or dates or numeric values that need to be treated as strings. So we need CAST. We will use CAST even if the field is already VARCHAR or CHAR, because we want to constrain the length of the result to a maximum of 3. Here also is where we need to apply TRIM to extract just the leading non-blank characters in a CHAR field. Thus in your example, the 2-character value 45 is left-justified in a 3-character field, and TRIM ensures we will end up with 045 later.

Finally, after applying COALESCE, CAST, and TRIM, we append the resulting string to a string of three zeroes, and then just take the rightmost three characters:

select right('000'
          || coalesce(cast(
              trim(field1) as varchar(3))
                     ,'')
           , 3 )
    || right('000'
          || coalesce(cast(
              trim(field2) as varchar(3))
                     ,'')
           , 3 )
    || right('000'
          || coalesce(cast(
              trim(field3) as varchar(3))
                     ,'')
           , 3 )
  from yourtable

If that looks strange to you, here's the Access version:

select right('000'
           & iif(isnull(field1), ''
                 , trim(field1))
           , 3 )
     & right('000'
           & iif(isnull(field2), ''
                 , trim(field2))
           , 3 )
     & right('000'
           & iif(isnull(field3), ''
                 , trim(field3))
           , 3 )
  from yourtable

For More Information


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL
IN list or series of OR conditions?
Connecting tables in a database
SQL query for co-authored books
Querying complex derived tables
SQL string functions
Changing a NULL column to NOT NULL
SQL for hourly totals for the last 48 hours
LEFT OUTER JOIN to a MIN/MAX row
Normalizing a crosstab table
Querying metadata and data at the same time

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts