9

Is it possible to make a new column, for example named "Test" and store the first part of the column "Name" split by -?

See below how it should look like:

enter image description here

  • 1
    Couldn't you use python's .split() function? – Branco Dec 22 '14 at 20:53
14

Yes you can.

Use the Field calculator with the following expression:

left( "Name", strpos( "Name" ,'-'))

The strpos() function will return the index position of the first '-' character and the left() function "trims" the string before that position.

enter image description here

  • That works, but "Name".split('-')[0], per @Branco, might be easier. – recurvata Dec 23 '14 at 14:01
  • Can you use it in field calculator? Otherwise you need to use the python console to iterate over the features. Compared to that, the field calculator looks more straightforward. There might be better expression using regular expressions. – Alexandre Neto Dec 23 '14 at 14:41
  • 3
    You can not use it in filed calculator. So answer provided by Alexandre Neto is more straight forward. But is better to use it with "-1" to avoid adding your charater to output. For example: left( "Name", strpos( "Name" ,':')-1). Otherwise you will end up with "3833-" not just "3833" – Losbaltica Jun 12 '17 at 13:49
4

You can use the Field calculator with the following expression:

string_to_array("Nombre", ' ')

enter image description here

The string_to_array() function splits string into an array using supplied delimiter.

If you want a some position of the array, you can add the position and it will return the value. like this:

string_to_array("Nombre", ' ')[2]

enter image description here

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.