9

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.

nmtoken
13.6k5 gold badges39 silver badges91 bronze badges
asked Sep 17, 2016 at 18:48
1
  • Definitely you must treat the postcode fields as Strings / text. Commented Sep 19, 2016 at 8:01

1 Answer 1

10

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'

answered Sep 17, 2016 at 19:26
4
  • 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/… Commented Sep 18, 2016 at 5:41
  • thanks for pointing this out ... Was late when I answered ;-) Commented Sep 18, 2016 at 9:16
  • I am not sure, but enclosing the string values between double quotes "0082" in the CSV file could be enough. Commented Sep 18, 2016 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. Commented Nov 6, 2017 at 23:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.