6

I´m working with QGIS 2.12. Is it possible to split string "from the right side" (it means I needed the last text from every field)?

In one field I have description like:

description
new street 25
old street 2
lower 26A
lower new street 125
Jozefs and Elisabeths new gardens 147A

Every line has a different count of texts. I needed a column with numbers:

numbers
25
2
26A
125
147A

Could be possible in QGIS?

1
  • Sorry for continuing on this question here but I am not allowed to comment yet. I would like to do the opposite, that is: new street 25 = new street old street 2 = old street lower 26A = lower lower new street 125 = lower new street Jozefs and Elisabeths new gardens 147A = Jozefs and Elisabeths new gardens
    – Nedim
    Commented Oct 12, 2017 at 11:04

2 Answers 2

9

You can do it using the following formula in the Field Calculator:

regexp_substr("Field_Name",'(\\d+|\\d+.+)') 

Where:

The first \ is to escape \d

\d+ : means extract one or more digits.

| : means OR.

\d+.+ : means extract one or more digits and one or more any other character.

It will give you the following results:

numbers
25
2
26A
125
147A

Even if you have text with a name 'Text 123456789ABCDEF', the output will be:

123456789ABCDEF

4
  • Thank you very much for your help, but the syntax does not work properly. If the last text is shorter than 2 digits (old street 1, new street 5....), than in the new column is written "NULL". The last digits (number of the house) could be combination: 1 / 1A / 11 / 11A / 100 / 100A. thank you
    – lucy147
    Commented Jul 11, 2016 at 11:13
  • I updated my answer to solve the problem if the last text is shorter than 2 digits.
    – ahmadhanb
    Commented Jul 12, 2016 at 0:53
  • @ahmadhanb What do these (\\d+|\\d+.+) characters mean in your expression?
    – Skye
    Commented Jul 28, 2016 at 8:47
  • 1
    @Skye "\\d+" means extract one digit or more digits, but the first \ is to escape \d. "|" means OR. "\\d+.+" means extract one or more digits and one or more any other character.
    – ahmadhanb
    Commented Jul 28, 2016 at 9:06
4

In the Field Calculator:

right("column", strpos("column", ' '))

Should do the job.

1
  • Thank yo very much for your answer, but the syntax does not work properly in every field. the results were 25 - r 26A - s 147A - t 125 - t 2. PC added the last letter in many fields.....
    – lucy147
    Commented Jul 11, 2016 at 11:05

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.