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:


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

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

| improve this answer | |
  • 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

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

| improve this answer | |

Possible solution by means of PyQGIS.

Let's assume there is a point layer called "some_points" with it's attribute table, see image below.


Proceed with Plugins > Python Console > Show Editor and paste the script below

from PyQt5.QtCore import QVariant

layer = iface.activeLayer()
if not layer.isValid():
    print("Layer failed to load!")

layer_provider = layer.dataProvider()
layer_provider.addAttributes([QgsField("Test", QVariant.String)])



for f in features:
    attrs = {2: f['info'].split('-')[0]}
    layer_provider.changeAttributeValues({f.id(): attrs})


The output will be looking



| improve this answer | |

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.