Solved

CSV file truncating 0's on left

Posted on 2013-12-04
11
264 Views
Last Modified: 2013-12-09
I have an excel spreadsheet. In the first column I have numbers with padded 0's, in another column I have the last 4 of the SSN:

025
026
099
100

When I save the file as a CSV file, it is removing my padded zeros:

25
26
99
100

I want to retain the 0's and have it NOT remove the 0's for both columns. How do I do that?

thanks!
0
Comment
Question by:Starr Duskk
  • 5
  • 5
11 Comments
 
LVL 39

Assisted Solution

by:nutsch
nutsch earned 500 total points
ID: 39696822
Are the zeros removed if you open the csv in notepad after saving? By default, excel upon opening a csv will guess at the data type. In your case, it converts the ssn string to a number.
You can avoid that by opening the csv through Data \ Get External Data from Text and specify the column type as text for your ssn column.
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 39696945
I am using this spreadsheet for an import. It is importing without the leading zeros.

I want those columns saved as text. No one will be opening it. It will be uploaded to a website, and the website will open it and import the data.
0
 
LVL 39

Expert Comment

by:nutsch
ID: 39696970
How's the file originated, how are you importing it, and do you have the leading zeros if you open the csv in notepad before opening in excel.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Accepted Solution

by:
Starr Duskk earned 0 total points
ID: 39697130
Nevermind. My research has shown that csv format wipes out leading zeros and there's nothing can be done.
0
 
LVL 39

Expert Comment

by:nutsch
ID: 39697168
Bob, thanks for giving me points in your closing, but I respectfully disagree.
CSV files do not by default wipe out leading zeroes. A csv file opened in excel with default settings will not have any leading zeroes, yes, but the csv file is just a text file with commas (or semi-columns in some countries) separating the fields. Nothing in the .csv format entails the loss of the leading zeroes.
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 39697188
In my situation, saving an excel file as a CSV file does indeed truncate the leading zeros on the fields with numeric data.
0
 
LVL 39

Expert Comment

by:nutsch
ID: 39697204
Do you achieve your leading zeroes by way of a custom format, such as "0000"? If you do, that's why it doesn't transfer to csv since the underlying data does not have the leading zeroes, and only the display has them.

If you convert the numbers to text, for instance by using the formula =text(A1,"0000"), then it should save to csv with the leading zeroes (as long as you don't reopen it with excel).
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 39697900
Thomas,
A csv will actually save using the displayed values, so a custom format will still work. I think your original hunch was correct.
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 39698778
Our clients are exporting text data to an Excel spreadsheet, then saving it as a CSV. They wont' be using formulaes because they already struggle with just that much.

In their database, the field is "0048" for SSN.

Then when they save it as a csv, it saves with "48" because it's dropped the leading zeros.

We've fixed this on the import by padding the data.
0
 
LVL 39

Expert Comment

by:nutsch
ID: 39698980
One final question on the subject, you say
Then when they save it as a csv, it saves with "48" because it's dropped the leading zeros.
How do you see that it saves as "48"? By opening the file in excel, or by opening it in notepad or another text editor?
0
 
LVL 2

Author Closing Comment

by:Starr Duskk
ID: 39705725
what I want to do isn't possible.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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