Add leading 0's

Posted on 2007-10-15
Last Modified: 2011-09-20
how do I add a leading 0 to all records in a column of type char?

exisiting data format  = 171347, 171331, etc
required data format = 0171347, 0171331, etc.

Question by:kpwhitte
    LVL 16

    Accepted Solution

    select '0' + column from tablename

    update tablename
    set column = '0' + column
    LVL 11

    Expert Comment

    It also might be good to mention that if the field is a numeric datatype, you'd have to convert it to a character type or always select it back out adding the zero and converting it to a character:

    SELECT '0' + convert(varchar, ColumnName) from tablename

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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

    I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
    I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    760 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

    8 Experts available now in Live!

    Get 1:1 Help Now