1

I am working in QGIS, which is quite new to me.

I have a text string containing dates.

I want to have new column where only the year from the text-date string appears. How is that possible. See picture below:

enter image description here

0

1 Answer 1

3

You can use the field calculator.

You would first extract the 4 rightmost characters, then convert them to an integer:

to_int(right(dato,4))
7
  • Won't they need single quotation marks around the field name, like this 'dato'? Otherwise I think it will just try to convert the word dato to an integer, and deliver a NULL result.
    – csk
    May 2, 2019 at 16:42
  • @csk no, with single quotes it would be the string 'dato', not the field name. It works without any quotes or with double quotes
    – JGH
    May 2, 2019 at 17:39
  • Hm. Field names working without quotes must be a new feature in QGIS3.
    – csk
    May 2, 2019 at 17:43
  • @csk I just tried it in 2.18 and it works too (open a shapefile/temp layer, calculator, new field, expression is right( fieldname ,2) and it does preview + populate).
    – JGH
    May 2, 2019 at 17:50
  • Interesting. I learned by using the built-in help in the Expression Builder, which puts field names in double quotation marks. Perhaps that's necessary when the field name has spaces, or matches another variable or function. I will have to pay attention to this in the future.
    – csk
    May 2, 2019 at 17:56

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.