How to use the field calculator to replace the character -
in part of the values with the character "B"
at the end of this value? I also want to replace the other values without symbol -
replace with the symbol "F"
at the end of these values. Like this -0345
to 0345B
, 0341
to 0341F
.
2 Answers
You can use regexp_replace
function in field calculator:
The expression
regexp_replace( "linecode", '^-(.*)$', '\\1B')
will replace '-' + 'any string' with 'any string' + 'B'. So it will change string '-0345' to '0345B'. \\1 refer to the expression part in brackets. You may give more specific regexp instead of '.*'.
The second expression will add 'F' to numeric strings:
regexp_replace( "linecode", '^([0.9][0-9]*)$', '\\1F')
It will replace only string made of numeric characters [0.9], so you can use it after the previous expression without selection in the table.
-
My answer is easy to understand, but this is more professional. :) Dec 22, 2020 at 13:00
Use this expression: if(left(linecode, 1) = '-', substr(linecode, 2) + 'B', linecode + 'F')
I suggest to create new field instead of changing the existing one. You may need the original one later.
linecode
, integer or string(text)?