4

Is there any good expression to extract the first word of a field and put it into a new column? I would like to create a new column without the numeric values and filter out words that are doubled.

This is what I intend to achieve

Input Desired output
Gebäude- und Freifläche Land- und Forstwirtschaft 13304 Gebäude- und Freifläche Land- und Forstwirtschaft
Landwirtschaft 26 Landwirtschaft
Gehölz 327 Gehölz
Landwirtschaft 2219 Landwirtschaft
Landwirtschaft 7318 Landwirtschaft
Fließgewässer 37 Fließgewässer
Landwirtschaft 574 Landwirtschaft
Landwirtschaft 6619 Landwirtschaft

Here is the attribute table of my layer

enter image description here

6
  • 2
    try this string_to_array("Nutzung", ' ')[0]
    – Taras
    Commented Nov 10, 2021 at 12:09
  • 3
    Do you really want it in a new row or do you mean a column? Commented Nov 10, 2021 at 12:19
  • True, I ment a new column. The expression from Taras would already work but I found out, that some fields include more than 2 words, so using the space as the operator doesnt work that good. Is there a way to filter out only the number at the end?
    – lesto
    Commented Nov 10, 2021 at 12:53
  • 4
    Removing all numeric values would be: regexp_replace("Nutzung", '\\d', '') Commented Nov 10, 2021 at 13:20
  • 1
    If you edit your initial post with the latter cases and explain what you want (I guess for the first case you want "Gebäude- und Freifläche Land- und Forstwirtschaft" and maybe for the next one "Landwirtschaft, Gehölz" - this is doable in the field calculator but a bit more work. Commented Nov 10, 2021 at 13:20

2 Answers 2

5

Use regular expressions to find the first digit, then return the input string from the first character until the character before the first digit using this expression:

left("Nutzung", regexp_match("Nutzung", '\\d') - 1)

enter image description here

3

May be:

In the field calculator it seems to work

substr("Nutzung", 0, strpos( "Nutzung", ' ')-1)

enter image description here

2
  • 1
    Looking at the desired output in the table of the question, this does not work as intended: input ``Gebäude- und Freifläche Land- und Forstwirtschaft 13304` should return Gebäude- und Freifläche Land- und Forstwirtschaft - with your expression, it just returns Gebäude-.
    – Babel
    Commented Nov 11, 2021 at 9:13
  • @Babel You have right. But before the post was edited (and the table added), it was desired the extraction of only the first word only Commented Nov 11, 2021 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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