2

I got a question regarding removing and/or creating a new field just with the 4 first characters of a field. Whenever I search for this I just find answers about how to remove a specific number of values from the front or the back, the problem here is that I have fields with a 4 digit number and then different names! like for example "2481 New York" and the next one "4521 Seattle" and I want a new field with only the first digits!

1
  • 1
    Something like : left(fieldname , 4) should do it...
    – J.R
    Nov 11, 2020 at 22:55

1 Answer 1

2

There are a few possible options to achieve the desired result.

You could use a substring function to extract the first 4 characters:

substr("YourFieldName",1,4)

Another possibility is to use the following regex function which extracts numbers appearing at the beginning of a string:

regexp_substr("YourFieldName",'^\\d+')

The advantage of the last one is that it can handle cases with varying number lengths.

I recommend to test this in a new field first before potentially updating your actual field, to check that the result is as you intended.

3
  • Thank you!! Even though they worked both of them, another problem occured haha! Some of them begins with a "0" so if its 0114 New Jersey, it becomes 114 only! The others starting with a 1.2.3 etc worked like a charm
    – Isaiah
    Nov 11, 2020 at 23:32
  • @John Then make sure your new field is of type 'text', and not a number-related type.
    – FSimardGIS
    Nov 11, 2020 at 23:34
  • Thank you! that worked perfectly!
    – Isaiah
    Nov 12, 2020 at 1:19

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.