11

Within an attribute table I'm trying to replace one string with another using the 'replace' function . As an example, there are a number of features whose field 'NAME' contain the string '(B)' and I'm wanting to replace this with the word 'County'. I'm selecting the 'update existing field' checkbox and using the following expression

 replace( '(B)','(B)','County')

The end result is that the field 'NAME' for every feature is replaced by 'County' irrespective of whether or not the field 'NAME' originally contained the string '(B)'. Any help would be appreciated.

  • GrahamD, thanks for the post and underdark, thanks for the answer. I have exactly the same challenge. p abbreviated for pan. Type_2 as a test column. text copied and pasted from The expression box: replace("Type_2", "p", "pan") all text and quotations in red. ok button greyed out. Output preview: expression invalid Parser Error: (nothing detailed) Eval Error: Column 'p' not found I get exactly the same results should I swop Type_2 and p around. underdark, any further suggestions would be gratefully appreciated. Thank you – VeM Nov 27 '13 at 8:37
20
replace( '(B)','(B)','County')

tells QGIS to replace the '(B)' in '(B)' with 'Country'.

You want

replace("Name",'(B)','Country')

= replace the '(B)' in "Name" with 'Country'.

Note that the double quotes indicate a field name while the single quotes refer to a static string. These are not interchangeable.

  • @GrahamD please accept the answer if it solved your problem – underdark Jan 9 '17 at 20:53
6

The proper syntax is replace([field_name], '(B)', 'Country')

3

Firstly, open field calculator and select the replace function from string oenter image description hereption like the following picture:

Secondly, tells QGIS to replace the replace ("Field Name",'(B)','Country'). For example in this case; replace ("MES_TIP", 'T', 'Rock'):

Thirdly, please choose the output field type: text(string) and then determine field name;enter image description here

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.