12

I have a column in my CSV file that contains a string of numbers separated by commas. Excel keeps converting them to numbers even though I want to treat it as text.

Example:

470,1680 get converted to 4,701,680

However, I want it to stay as 470,1680

I tried to format the cells as text but that removes the original comma. How can I achieve this?

John Y
  • 14,123
  • 2
  • 48
  • 72
MagentoMan
  • 553
  • 4
  • 8
  • 19
  • 1
    Do you still have the original CSV file? – Jerry Jan 03 '14 at 17:07
  • @Jerry Yes In the source it shows correctly. Just not while viewing. – MagentoMan Jan 03 '14 at 17:19
  • Can you give us a full sample line or two from your CSV? – John Chrysostom Jan 03 '14 at 17:23
  • possible duplicate of [string (123) in Excel csv file](http://stackoverflow.com/questions/5703781/string-123-in-excel-csv-file) – John Y Jan 03 '14 at 17:39
  • Yes, that is how it is converting it. However, if you look at the source it is formatted differently. – MagentoMan Jan 03 '14 at 18:11
  • Another possible duplicate: http://stackoverflow.com/questions/308324/csv-for-excel-including-both-leading-zeros-and-commas – John Y Jan 03 '14 at 18:25
  • I am exporting this using Navicat. Maybe someone knows how to make it stay as text during the export? – MagentoMan Jan 03 '14 at 18:29
  • There is no such thing as "make it stay as text during the export" because CSV *inherently has no concept of numeric vs. text*. How the data is interpreted is utterly and completely dependent on the thing that is *reading* the data, not on the CSV file itself. Your best bet is to use some kind of scripting language (Python, Perl, etc.) to process the raw CSV, *before* Excel gets its grubby little hands on it. – John Y Jan 06 '14 at 18:29
  • Oh hey, I just checked out Navicat's Web site and apparently there is an option to export directly to Excel format (rather than CSV). And this *does* have a chance at "making it stay as text during export". So try this! – John Y Jan 06 '14 at 18:32

7 Answers7

8

What I found that worked was this:

="12345678901349539725", "CSV value2", "Another value"

The key here is that this value is a string containing ="{Number}". Somehow, Excel respects that pattern.

Perhaps it could be better written as

"="12345678901349539725"" 

But don't go crazy with the quotes in your code.

Harvey A. Ramer
  • 763
  • 7
  • 13
7

Rename the .CSV file to a .TXT file. Open the file with Excel, and the text import wizard will pop up. Tell Excel that it's a delimited file and that a comma is the delimiter. Excel will then give you a screen that allows you to assign formats to each column. Select the text format for the column in question. Import and you're done!

To test this, I created the following .CSV file:

test1,"470,1680",does it work
test2,"120,3204",i don't know

When opening the CSV directly in Excel, I get the following:

test1    4,701,680    does it work
test2    1,203,204    i don't know

When opening using my method, I get this instead:

test1    470,1680    does it work
test2    120,3204    i don't know

Is this not the desired result?

John Chrysostom
  • 3,973
  • 1
  • 34
  • 50
  • Yes, I have. Yes, it works. Just to be anal, I just did it with that value. Everything is peachy. – John Chrysostom Jan 03 '14 at 17:15
  • Doesn't work, thanks though. The columns did not align correctly. – MagentoMan Jan 03 '14 at 17:17
  • What's not working properly? I literally just did this on my machine at work. I'll be happy to help out if I can. – John Chrysostom Jan 03 '14 at 17:18
  • Obviously not... that would produce 5 columns, because Excel doesn't know which commas are "real" delimiters and which are not. But that's beyond the scope of OP's question. – John Chrysostom Jan 03 '14 at 17:24
  • Seems like your idea would work but I can't get the columns aligned correctly. – MagentoMan Jan 03 '14 at 17:32
  • Hmmm... how are they not aligning correctly? Is it splitting up the numbers in that one column that you want to keep together? If so, you may be working with an improperly formatted CSV file. Any field in a CSV file containing a comma must be surrounded by quotes... this tells the computer "hey this quote is part of the data, not a delimiter!" We may be able to help with the alignment issue if you can post a line or two from the file. – John Chrysostom Jan 03 '14 at 17:33
  • That is what I was thinking but each column is double quoted and comma separated. EX: "510,1850", – MagentoMan Jan 03 '14 at 17:36
  • Ah... great. I see the problem. There are extra line breaks in the CSV and Excel treats an end of a line as an end of a row when you read in with the text import wizard... let me see if I can come up with a workaround. – John Chrysostom Jan 03 '14 at 18:22
2

If you can manipulate CVS file put ' in front of each number

Emmanuel N
  • 7,350
  • 2
  • 26
  • 36
0

OK... so, the file is using carriage return + line feed characters to delineate the beginning of a new record. It also (for reasons I don't understand) has line feed characters within each record at random places - but there are no carriage returns.

To fix this, I opened the file with Notepad++, and did a find and replace with "Extended" search mode. I replaced \n with nothing. The data now opens in Excel properly using my earlier recommended solution.

You can, of course, use any other program (not just Notepad++) to make this character substitution. Does that help?

John Chrysostom
  • 3,973
  • 1
  • 34
  • 50
0

Try this where DocNumber is actually text : Select (CHAR(10)+DocNumber) AS DocNumber

That is by adding an invisible text char it fools Excel into making it a Text string. You can use CHAR(32) too.

0

The problem is about Excel thousands separator. My quick solution is simple and worked for me.

  1. Go to Excel-->File-->Options-->Advanced
  2. Find "Thousands separator". Probably your separator is ",".
  3. Change the separator like "x", etc.

After you are done, I recommend to switch the separator as "," back.

Ege
  • 1
0

After trying random sets of chars I confirm that the method:

="003"

worked like a charm for any strange thing to put into those quotes: numbers, dates... It is the exact way to tell Excel that this value from CSV is a string. I needed that as CSV is easy to generate in a program, but user would open the file automatically via Excel.