SQL field padding.

Posted on 2007-07-23
Last Modified: 2012-08-14
I am writing a Transact sql query that will produce data for a fixed length flat file. Is there anyway to pad the data with the appropriate number of blank spaces in the query? I can handle this in code but would prefer to in the query if it is possible.

For example: There are 166 fields and the total character length per row must be 1600 not more or less. Some fields may have no data but must contain the right amount of padding or the field may have data such as 10 chars worth but the entire field length must be 20.
Question by:jfsedlar3rd
    LVL 15

    Accepted Solution

    If you need specific field lengths, you should use char instead of varchar for your column datatypes. That will automatically pad the data as needed.
    LVL 10

    Assisted Solution

    in your Query you will need a bunch of these:
    Cast(Isnull(<yourfield>,'') as Char(x))
    where <yourfield> is your field name, x is the lengh you want the field to be.
    It will pad the field with spaces for you.  You need the Isnull in case you have null fields and don't want a null value

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now