By following this question:

QGIS set fixed ID name with autoincrement in data attribute table

I tried to prepare the formula, which could let me increment the very last number in the string.

The ID is: VON/FLN/CBF-DU2/A1/VP.1 and I need the increment like: VON/FLN/CBF-DU2/A1/VP.2, VON/FLN/CBF-DU2/A1/VP.3, VON/FLN/CBF-DU2/A1/VP.4, etc

So far, my formula looks as follows:

'VON/FLN/CBF-DU2/A1/VP.' || lpad(to_string(maximum(to_int(right("ID", 3)))+1), 3, '0')

but it keeps throwing the NULL value in my data attribute table.

The same is with the following formula:

'VON/FLN/CBF-DU2/A1/VP.' ||  lpad("pkuid", '3', '0')

and this one

IF("ID" IS NULL, 'VON/FLN/CBF-DU2/A1/VP.' || lpad(to_string(maximum(to_int(right("ID", 3)))+1), 3, '0'), "ID")

Does anyone know where might be the problem?

How to use the


function effectively?

enter image description here


   IF("ID" IS NULL, 'VON/FLN/CBF-DU2/A1/VP.' || 
   lpad(to_string(maximum(to_int(right("ID", 1)))+1), 2, '0'), "ID")

After changing the "ID", 3 to "ID", 1 the formula has started to work. Could anyone explain what do these values mean? Considering that they bounded with function right and to_int?

  • Sorry, as long as you don't tell us what kind of values you have in the field ID (is there alreade an increment number inside this field? If not: what else?) and what kind of problem you get with the formula you present, it is difficult to answer. Can you post a screenshot of your attribute table? Why do you include a maximum() function?
    – Babel
    Feb 9, 2022 at 11:28
  • OK, I added the shot. As you can see I need the increment such as this, but despite lack of error the function throws NULL and I am wondering where the problem is.
    – Geographos
    Feb 9, 2022 at 11:34
  • The expression should work, even without the maximum part. I guess you simply defined the wrong field type. Is the field you want to create formatted as string and long enough?
    – Babel
    Feb 9, 2022 at 11:35
  • You can't convert characters to integer, hence to_int(right(ID,3)) doesn't return anything.
    – Erik
    Feb 9, 2022 at 11:35
  • I've returned to_int with all brackets but it still throws NULL
    – Geographos
    Feb 9, 2022 at 11:39

2 Answers 2


I am not an expression expert, but I think the following script helps:

layer = iface.activeLayer()

layer.dataProvider().addAttributes([QgsField("ID", QVariant.String, len=50)])

request = QgsFeatureRequest().addOrderBy("pkuid")
for i, feat in enumerate(layer.getFeatures(request)):
    lpad = f'{i+1:03}' # returns 001, 010, 099, 100, ..
    feat["ID"] = 'VON/FLN/CBF-DU2/A1/VP.' + lpad

enter image description here

  • I really appreciate your help. Thank you. The answer has been accepted and upvoted, as this approach can be a good alternative. Anyhow if someone could solve it on the basis of the expression builder that would be also appreciated.
    – Geographos
    Feb 9, 2022 at 12:25

To do that with QGIS expressions, use a replace() function and replace the constant string (everything before the final increment id number) with an empty string. So just the incrementing number is left, regardless how many digits it has:

'VON/FLN/CBF-DU2/A1/VP.' ||  
lpad( to_string (to_int (replace("ID", ('VON/FLN/CBF-DU2/A1/VP.'),''))+1),3,0)

enter image description here

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.