3

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'?

1 Answer 1

7

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
  • 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. Commented Apr 26, 2017 at 9:54
  • @MattiVirkkunen you are quite right, I've fixed it
    – Ian Turton
    Commented Apr 26, 2017 at 10:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.