Does anyone know how to add leading zeros to a CSV file?
![]() |
Patch reliability is unclear, but widespread attacks make patching prudent. Go ahead and patch, but watch out for potential problems. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
CSV Files (1)
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » CSV Files (1)
- This topic has 28 replies, 8 voices, and was last updated 22 years, 10 months ago.
Viewing 3 reply threadsAuthorReplies-
WSkieran
AskWoody LoungerJuly 23, 2002 at 3:17 am #602966Load the file into Excel (if it is not too large).
Use the text to colums feature to break the data into separate columns. Ensure each columns data type is set to text.
Then you can manipulate the colum you wat to add leading zero’s to. I suggest that you use a fourmula in another spreadsheet to do this eg =”00″ & A1 to add 2 leading zero’s, and then seelct the whole column and paste backinto the orginal using Paste Special and the values option.When all looks OK, save the fiel as CSV and you are finished.
-
WSkram
AskWoody Lounger -
WSAJarrell
AskWoody Lounger -
WSkram
AskWoody Lounger -
WSAJarrell
AskWoody LoungerJuly 23, 2002 at 2:53 pm #603166If the column is the first one in the CSV file, then the solution is a minor variation. Instead of replacing CR/LF/” combinations with CR/LF/=”, the replace changes to CR/LF/=”0.
If it is not the first column, then the solution may be simpler in a formula within Excel.
For example, if the entry is in column B, and the first entry is in B2:
=text(B2,”000#”)
Hope this helps.
-
WSkram
AskWoody Lounger -
WSkram
AskWoody Lounger -
WSAJarrell
AskWoody LoungerJuly 23, 2002 at 3:50 pm #603206If the first entry is in G2, then the formula is =text(G2,”000#”).
Copy that formula down, and the cell references will automatically change to reflect the appropriate row. For example, the second entry formula will be =text(G3,”000#”), and the third entry will be =text(G3,”000#”)
Regards.
-
WSkram
AskWoody Lounger -
WSJohnBF
AskWoody LoungerJuly 23, 2002 at 4:40 pm #603220A CSV is only supposed to save the values.
FWIW, I used HansV’s suggestion and exported an Excel file with a column of employee counts (all less than 1,000) within a table set as Format, Custom, 00000, to CSV. The CSV saves the data with the leading zeros, which can be seen by opening the file with Notepad. However, on reimporting the CSV into Excel, the leading zeros are lost and the column has to be reformatted.
[Edited] Sorry if that is unclear. I understand that you are trying to get an existing CSV to have the leading zeros. Open it in Excel then format the applicable columns, then re-export it, the CSV will have the leading zeros.
-
WSjstevens
AskWoody LoungerJuly 25, 2002 at 11:58 pm #604083Have your tried opening the CSV file in XL and formating the column with cell formats where the category is “custom” and the type “0000”? You should see all numbers displayed as four digits such as 100 being 0100.
You can save the CSV file with the new format. One thing though if you try to open the CSV file in XL once again it will lose the format you saved it under.
-
WSkram
AskWoody LoungerAugust 6, 2002 at 12:37 pm #606365I would like to thank everyone who responded to this posting, my apologies that I have not responded sooner as I have been on vacation. As it turned out the CSV file that was extracted from Oracle DID have the leading zeros; however, when we opened the CSV file in XL it lost the format we saved under.
-
-
-
-
-
WSHansV
AskWoody LoungerJuly 23, 2002 at 7:28 am #603023If you want to save numbers with a fixed number of digits (e.g. 1 becomes 0001, 23 becomes 0023, 456 becomes 0456 and 9876 becomes 9876), you can simply format them in Excel as “0000”. Exporting to CSV will preserve this format.
Or do you want to add leading zeroes to an existing CSV file?
-
WSkram
AskWoody Lounger -
WSAJarrell
AskWoody Lounger -
WSkram
AskWoody Lounger -
WSAJarrell
AskWoody LoungerJuly 23, 2002 at 1:29 pm #603133If the column with the need for the leading zero is the first column, then open up the CSV file with a good text editor, and replace the CR/LF/” entries with a CR/LF/=” entry. (Of course, you would have to change the first line manually.) If it is not, then you may want to replace all ,” with ,=”.
This second choice will play havoc with any other numeric values in the file. But they can be addressed via value() formulas afterwards.
Hope that helps.
-
-
-
-
-
WSGARYPSWANSON
AskWoody LoungerJuly 23, 2002 at 8:21 pm #603287I have done this in the past as follows:
– Import the csv file into excel.
– Insert a column next to the field you need to add leading zero’s too.
– Create a nested if then statement that states
— If length of field needing leading zeros is 4, then ‘0, 3 then ’00, 2 then ‘000 1 then ‘0000, else ‘00000
—– this creates the adding zero’s needed. The ‘ is required to make this work.
– Copy and the Paste the column on itself using the Paste Special, values to get rid of the if then statements.
– Insert another column and then concatenate the leading zero’s with the original column to give you ‘0100.
– Copy the concatenated data and paste special on the original field with 100.
– Delete extra columns
-Re-save the data as a .csv.You will not lose the leading zeros doing it this way and the leading zero’s will still be there if you re-import the data into excel.
Hopefully, this will give you some other ideas.
HTH – even if it is a bit manual…. -
WSkram
AskWoody Lounger -
WSGARYPSWANSON
AskWoody Lounger -
WSHansV
AskWoody Lounger -
WSGARYPSWANSON
AskWoody Lounger
-
-
WSAJarrell
AskWoody Lounger
-
-
WSMario
AskWoody LoungerJuly 24, 2002 at 1:15 pm #603553I’ve been doing something similar to GARYPSWANSON but without using the if formula
I have done this in the past as follows:
– Import the csv file into excel.
– Insert a column next to the field you need to add
leading zero’s too.
– change the 101,102,103 to numbers and do an sum example +G1+1000000
– Copy and the Paste the column on itself using the
Paste Special, values to get rid of the sum
– Do a copy paste special and select fixed width and
decide how many digits it should be
– on the next screen select text
– Copy data and paste on the original field with 100.
– Delete extra columns
-Re-save the data as a .csv.
-
-
-
WSHansV
AskWoody LoungerJuly 24, 2002 at 7:28 am #603445I think it’s time to do a little thinking. What do you want to do achieve with your CSV file?
You have been given several suggestions that will save numbers formatted with leading zeroes to a CSV file. The easiest one, I think, was my suggestion to open the CSV file, format a column with the custom format of 0000, and then save it. The saved file will contain the formatted numbers, as you can see when you open it in NotePad (JohnBF also remarked this). However, the formatting will be lost when you import the CSV file into Excel or Access.
There is probably a way of saving the values with an apostrophe ‘ in front of them. That way, they will be interpreted as text in Excel. But I think they will be useless for use in other applications. The purpose of CSV is to enable data interchange between different applications.
I don’t want to sound harsh, but I think it probably comes down to this:
If you want to have a file with formatting, save it as an Excel worksheet. Many programs can read Excel worksheets.
If you really need a CSV file, don’t bother with formatting the data. Do the formatting in the application in which you use it. -
WSGARYPSWANSON
AskWoody LoungerJuly 24, 2002 at 11:42 am #603473Hans,
I have to agree and disagree with your statements. From doing data conversions using .csv files as the required format to be submitted, if the apostrophe is not inserted as you state, the format is lost during the import of the data. This wreaks havoc on the input programs and causes the data to fall into incorrect categories and be misread. (I am speaking from doing volumes of conversion data from Oracle, to Excel / CSV and back into SAP.) — of course I may be looking to deep into this.
Other applications will ignore the apostrophe on import and treat the data as text as you indicate. Trying to format the data after import is also useless as the data is out of format and alignment. Once the data is imported into the system, the format can be re-established.
-
WSHansV
AskWoody Lounger -
WSGARYPSWANSON
AskWoody LoungerJuly 24, 2002 at 3:00 pm #603605… Hans,
If the conversion people had used access from the start, then we would never have encountered this problem. Actually, we never did figure out why they didn’t just transfer data from one Oracle database to another??? They ultimately went from each accounting system into Excel to save as a .csv and then imported the csv data into access for testing and then took the correct csv file into Oracle. (Kind of a long and odd way to do this)
We had to help many people to deal with the issue of losing leading zero’s on fixed width columns of data for the conversion process.
As always, thanks for your input and suggestions, … and I return the bow to your knowledge of access and other programs (which far exceeds my knowledge).
-
-
-
Viewing 3 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
MS-DEFCON 3: Businesses must tread carefully
by
Susan Bradley
3 hours, 11 minutes ago -
McLaren Health Care says data breach impacts 743,000 patients
by
Nibbled To Death By Ducks
3 hours, 43 minutes ago -
WhatsApp banned on House staffers’ devices
by
Alex5723
4 hours, 59 minutes ago -
Is your device eligible?
by
Susan Bradley
6 hours, 43 minutes ago -
Windows 11 Insider Preview build 26200.5661 released to DEV
by
joep517
12 hours, 54 minutes ago -
Windows 11 Insider Preview build 26120.4452 (24H2) released to BETA
by
joep517
12 hours, 55 minutes ago -
Hello Windows…My Problem is Windows Hello…
by
rdleib
14 hours, 11 minutes ago -
New Canon Printer Wants Data Sent
by
Win7and10
14 hours, 29 minutes ago -
I set up passkeys for my Microsoft account
by
Lance Whitney
3 hours, 59 minutes ago -
AI is for everyone
by
Peter Deegan
14 hours, 2 minutes ago -
Terabyte update 2025
by
Will Fastie
8 hours, 5 minutes ago -
Migrating from Windows 10 to Windows 11
by
Susan Bradley
3 hours, 38 minutes ago -
Lost sound after the upgrade to 24H2?
by
Susan Bradley
1 day, 13 hours ago -
How to move 10GB of data in C:\ProgramData\Package Cache ?
by
Alex5723
3 hours, 7 minutes ago -
Plugged in 24-7
by
CWBillow
23 hours, 13 minutes ago -
Netflix, Apple, BofA websites hijacked with fake help-desk numbers
by
Nibbled To Death By Ducks
2 days, 2 hours ago -
Have Copilot there but not taking over the screen in Word
by
CWBillow
1 day, 23 hours ago -
Windows 11 blocks Chrome 137.0.7151.68, 137.0.7151.69
by
Alex5723
3 days, 17 hours ago -
Are Macs immune?
by
Susan Bradley
9 hours, 4 minutes ago -
HP Envy and the Function keys
by
CWBillow
3 days, 1 hour ago -
Microsoft : Removal of unwanted drivers from Windows Update
by
Alex5723
18 hours, 33 minutes ago -
MacOS 26 beta 1 dropped support for Firewire 400/800
by
Alex5723
4 days, 5 hours ago -
Unable to update to version 22h2
by
04om
1 day, 13 hours ago -
Windows 11 Insider Preview Build 26100.4482 (24H2) released to Release Preview
by
joep517
4 days, 12 hours ago -
Windows 11 Insider Preview build 27881 released to Canary
by
joep517
4 days, 12 hours ago -
Very Quarrelsome Taskbar!
by
CWBillow
3 days, 22 hours ago -
Move OneNote Notebook OFF OneDrive and make it local
by
CWBillow
5 days, 1 hour ago -
Microsoft 365 to block file access via legacy auth protocols by default
by
Alex5723
4 days, 14 hours ago -
Is your battery draining?
by
Susan Bradley
20 hours, 12 minutes ago -
The 16-billion-record data breach that no one’s ever heard of
by
Alex5723
1 day, 13 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.