5

I have a set of features in a single layer that have been created during various periods of time.

In a layer attribute table, there is a column kreirao_datum which means created on a date

In a layer attribute table, there is a column kreirao_datum which means created on a date

I would like to select all features created within a single calendar year, for example 2021, using 'Select by Expression', but I can not figure out how to write the expression.

It should probably be something like "kreirao_datum" = '2021-07-29T09:45:01.000', and it works for a single matching record.

But how can I write expression that would select all records created within a 2021 year, "kreirao_datum" = '2021_????'?

enter image description here

I am very well aware that I can use Filter within attribute table, and it also works, but I am trying to determine what would be the right expression for 'Select by Expression'.

2 Answers 2

6

Use function year() to extract the year form your date:

year ("kreirao_datum") = 2021

enter image description here

3

@Babel has the right answer because this is a neat datetime column but if there wouldn't be (or if there is some weirdness going on below the screenshot) there are more tools available under the "String" section.

enter image description here

Mostly, the left, right and substr functions can clip out parts of the date string. If you are really desperate you can use the various regex functions or strpos to search the column for the year but this has a danger and might match a month-day like 2002 (20 februari) with the year.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.