Padding with zeroes
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?

    Requires Free Membership to View

    By submitting your registration information to SearchOracle.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchOracle.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

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


This was first published in February 2004

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.