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– Alexandre Neto2016年09月19日 08:01:22 +00:00Commented Sep 19, 2016 at 8:01
1 Answer 1
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– AndreJ2016年09月18日 05:41:48 +00:00Commented Sep 18, 2016 at 5:41 -
thanks for pointing this out ... Was late when I answered ;-)simo– simo2016年09月18日 09:16:49 +00:00Commented 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.user30184– user301842016年09月18日 16:06:05 +00:00Commented Sep 18, 2016 at 16:06
-
1I 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– Michael Workman2017年11月06日 23:17:47 +00:00Commented Nov 6, 2017 at 23:17