8

I'm new to QGIS, and Geographic Information Systems in general, and also have no experience with informatics.

I a have shapefile which works fine, and then I have a file in CSV format, and I try to join these two files, using a column which should be equal between two files. The problem is, some of the numbers have leading zeros (they are some sort of postcode in my country), that disappear from the CSV file when I import it to QGIS...

Is there any simple way to keep leading zeros?

In the shapefile the leading zeros don't disappear.

  • Definitely you must treat the postcode fields as Strings / text. – Alexandre Neto Sep 19 '16 at 8:01
9

You can specify type values by using a CSVT file, as follow :

"Integer(6)","Real(5.5)","String(22)"

If your csv file is named data.csv you will name your csvt file data.csvt.

Anita Graser (here known as underdark) has written a blogpost related to this. It is available at : https://anitagraser.com/2011/03/07/how-to-specify-data-types-of-csv-columns-for-use-in-qgis/

I would also refer to this Q/A for more information.

Edit : to be more precise, you should specify your column as "String" to keep leading zeros. An other option would also be to use lpad() function from calculator. See following Q/A.

For example : lpad('22', 4, 0), will return '0022'

  • I wonder who has invented that example, but it returns 2200 for me. Instead, I suggest left padding, as explained in gis.stackexchange.com/questions/208686/… – AndreJ Sep 18 '16 at 5:41
  • thanks for pointing this out ... Was late when I answered ;-) – simo Sep 18 '16 at 9:16
  • I am not sure, but enclosing the string values between double quotes "0082" in the CSV file could be enough. – user30184 Sep 18 '16 at 16:06
  • 1
    I also wanted to add that it is necessary to upload the .csv and the .csvt files at the same time in QGis via Layer --> Add Layer --> Add Vector Layer method. – Michael Workman Nov 6 '17 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.