18

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)'.

0

5 Answers 5

32
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.

1
  • 1
    @GrahamD please accept the answer if it solved your problem
    – underdark
    Jan 9, 2017 at 20:53
10

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

4

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

1

The interface looks a little different now and you have to select the column from the menu in the drop down on the left- abc name

I also noted that words in the single quotations have to be exact including no extra spaces. (which stumped me briefly)

I was replacing the word Road with Rd. in this example.

replace( "name", 'Road', 'Rd.' ) 

Press Update Filtered to make the change. It can take a while...as this OSM file did.

Filtering out a huge table would help speed up the replacement process.

enter image description here

0

There is also a small plugin available now, which wraps regex_replace in a small GUI:

Field find/replace

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.