TransWikia.com

Using the field calculator to replace the character in QGIS

Geographic Information Systems Asked on August 6, 2021

How to use the field calculator to replace the character - in part of the values with the character "B" at the end of this value? I also want to replace the other values without symbol - replace with the symbol "F" at the end of these values. Like this -0345 to 0345B, 0341 to 0341F.

enter image description here

2 Answers

You can use regexp_replace function in field calculator:

enter image description here

The expression

regexp_replace(  "linecode", '^-(.*)$', '1B')

will replace '-' + 'any string' with 'any string' + 'B'. So it will change string '-0345' to '0345B'. 1 refer to the expression part in brackets. You may give more specific regexp instead of '.*'.

enter image description here

The second expression will add 'F' to numeric strings:

regexp_replace( "linecode", '^([0.9][0-9]*)$', '1F')

It will replace only string made of numeric characters [0.9], so you can use it after the previous expression without selection in the table.

Correct answer by Zoltan on August 6, 2021

Use this expression: if(left(linecode, 1) = '-', substr(linecode, 2) + 'B', linecode + 'F')

I suggest to create new field instead of changing the existing one. You may need the original one later.

Answered by Kadir Şahbaz on August 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP