Sharpeye500 Sharpeye500 - 7 days ago 5
MySQL Question

Export to csv/excel way to include leading zero

Exporting select query data from mysql to csv & opening with excel, the leading zeros are gone.

Although the column is varchar, when the data gets exported, it writes like

4567 instead of 04567,
is there a way to preserve the leading zero while exporting to csv?

Answer

You need to wrap the number in double quotes like: ...,"04567",..., but then it will be interpreted as text in the spreadsheet so integer arithmetic will not work on it unless you do a conversion first (integers cannot begin with 0 in excel).