2

I would like to make a date out of a string (QGIS 3.22). For this purpose, I have created a new field into which the conversion is to be made with the following formula.

to_date(  "Erfassungsdatum" , 'MMMM d, yyyy')

I get the following error:

An error occurred while evaluating the calculation string:
Cannot convert '14.01.22' to Date

The field "Erfassungsdatum" is a string and the data is in the following example format: 14.01.22

Does anyone know a solution?

2 Answers 2

8

You are using the wrong date format. Use 'dd.MM.yy':

to_date("Erfassungsdatum",'dd.MM.yy')

If I reacall correctly, two digit dates are interpreted as 19xx by default, so you may need to add 100 years like + to_interval('100 years')

2
  • Thanks for the quick support, it works
    – Alex
    Commented Jul 13, 2022 at 9:11
  • 3
    Please accept the answer if it solved your problem: hit the checkmark near the answer.
    – Babel
    Commented Jul 13, 2022 at 9:14
3

According to the to_date function help :

to_date('June 29, 2019','MMMM d, yyyy') → 2019-06-29, if the current locale uses the name 'June' for the sixth month, otherwise an error occurs

so your formatting is wrong as your string don't have the name of the month but the number, you should use dd.MM.yy to reflect your date format (link to documentation)

this expression should work :

to_date( "Erfassungsdatum" , 'dd.MM.yy')

(make sure that the new field to update is of date type, also as the year in your string is only two character the expression will likely default to 1922 rather than 2022...)

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.