I have a dense attribute table of all the districts in the UK. I want to label the districts but each row in my 'NAME' field has 'District' after it. I wish to take this off, only leaving the name of the district e.g, Wycombe, South Bucks, Chiltern etc...

enter image description here

Is it possible to create a new field by writing an expression to eliminate all values that match 'district'?


The easiest way is to modify the expression used for the label to be:

  replace( "NAME" , 'District', '')

Or if you are using BoundaryLine then I recommend

regexp_replace( "NAME" , 'District.*$', '')

to deal with districts like 'Allerdale District (B)'.

Infact for completeness I use:

 wordwrap(  replace( 
    regexp_replace( "NAME" , 'District.*$|London Boro$', ''),'(B)',''), 10, ' ')

You can avoid the second replace if you can work out how to quote the ( & ) in (B).

You may also find the answers to my question on switching to numbers instead of names when labeling small polygons useful.

  • 2
    I haven't used QGIS, but shouldn't that be 'District.*$'? I'd imagine as it is now the * would apply to the the t. – Matti Virkkunen Apr 26 '17 at 9:54
  • @MattiVirkkunen you are quite right, I've fixed it – Ian Turton Apr 26 '17 at 10:06

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.