Tuesday, 24 December 2019

Trimming Specific Text Values in QGIS Field Calculator



Sorry if this is a dumb question, but I am wondering if there is a way to copy the text from one field to a new field, but eliminate specific words. For example:


**Old Field**                         **New Field**

Killbear Provincial Park Killbear
Quetico Provincial Park Quetico
La Cloche Provincial Park La Cloche
Wildcat Hill Provincial Park Wildcat Hill

I thought of trying a trim function, but not all the values have the same start location or trim length.


Thanks...




Answer



In your attribute table create a new string column and then use the field calculator as follows:


 replace(OLD_FIELD, 'Provincial Park','')

The problem with this approach is that your fields won't be trimmed, so you can do the following little hack to address that:


 replace(OLD_FIELD, ' Provincial Park ','')
replace(OLD_FIELD, ' Provincial Park','')
replace(OLD_FIELD, 'Provincial Park ','')
replace(OLD_FIELD, 'Provincial Park','')

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...