7

How can I in QGIS through Expression calculator remove leading zeroes i.e. the 0 leading from a string?

Original_Value New_Value
09 9
0150 150
09850 9850
054a23 54a23
5837 5837

Something like the example above.

0

4 Answers 4

11

The following should be working :

 regexp_replace("field_to_modify",'\\A0','')

Only if your aim is to strip a (=one) leading zero ...

4
  • 2
    It worked like a charm thank you! But what does \\A mean and where can I find the documentation for these?
    – Felippe M.
    Apr 8, 2021 at 18:29
  • \ stands for this rexegg.com/regex-quickstart.html#morechars and \A declares this rexegg.com/regex-anchors.html#A
    – Taras
    Apr 8, 2021 at 21:33
  • \A stands for 'start of string' ...just google 'regex tutorial' and you will find plenty of ressources ...
    – Snaileater
    Apr 9, 2021 at 8:21
  • Add an asterisc to the expression for one or more 0 (zero). regexp_replace("field_to_modify",'\\A0*','') Apr 9, 2021 at 12:37
6

Simply you can use the regexp_replace() function in the following expression:

regexp_replace("FieldName", '^0', '')

'^0' means select the 0 that is located at the beginning of string.

"FieldName" is the input field that you want to remove the zero from.

Here is the output:

enter image description here

1

I would also suggest making a number attribute column and populating this new column with "OLD_Column" * 1

1
  • 1
    The original column is a string with non-digit characters. Apr 9, 2021 at 12:38
1

Another way, if there are only numeric characters in your field and you have multiple leading zeros, is to convert the field to an integer using to_int("FIELD_NAME") so 0000535 becomes 535.

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.