I have data originating from a central registry for drilling wells. Each well may have a date attached to it and I want to create a layer which differentiates wells drilled by a certain year. Problem is the data are stored in a String format and it can look like yyyy or yyyymm or yyyymmdd or null.

So how can I create a function that takes the 4 leftmost characters if any and convert into a number like To_integer(Left4(MyValue)) so I can filter it out by those older than a certain year?


1 Answer 1


Such a function already exists with QGIS expressions: left( 'yourstring' , 4) returns 'your'. Use it with Field calculator.

Use to_int(left( 'yourstring' , 4) to convert to an integer: to_int(left ('2021-10-04', 4)) returns: 2021 as integer.

You could also use to_date to format your string as date: to_date(left ('2021-10-04', 4),'yyyy') returns 2021-01-01 in date format. To retrieve the year, use year: year (to_date(left ('2021-10-04', 4),'yyyy')) returns 2021.

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.