What is the right expression for the QGIS Field calculator to get value string-field like "new field"?
Asked
Active
Viewed 42 times
0
-
1In python you can slice the string, assuming all your 'field' values are the same length. https://www.w3schools.com/python/gloss_python_string_slice.asp – Michael Stimson Sep 25 '23 at 05:10
-
You can use the expression suggested in Extracting part of string from field in QGIS Field Calculator?. You need to change the range specified in the expression for each part of the string that you need to extract. – ahmadhanb Sep 25 '23 at 05:23
1 Answers
1
You can create a new string field front field within the Field calculator, populating it with the following expression
substr("field",1,4)
This way, you extract 4 characters, starting from the first one, of the field field. Similarly, you can create and populate middle field and back field with these other expressions
substr("field",5,5)
substr("field",10,4)
Please note that your example has 5 digits in the first row of the middle field but only four in the other two. If that was intentional, you should use a conditional statement similar to
case when "field" = '0104030002101' then substr("field",5,5)
else substr("field",5,4) end
jpinilla
- 3,230
- 1
- 12
- 24
