2

I have a string column with a datetime in it, I want to drop the time and reformat the column to date format. In field calculator I type:

to_date( (EndDate), 'yyyy/MM/dd') 

This gives the following error:

Eval Error: Cannot convert '2020-07-15 15:12:55' to Date

How do I manage this?

1
  • 2
    What is the current column type?
    – Erik
    Dec 17 '20 at 14:01
8

I advice you to use the format_date method like this:

format_date( "Enddate" , 'yyyy/MM/dd')

or the to_date method without specifying a format:

to_date( "Enddate" )

In both cases, the result is 2020-07-15.

3
  • 2
    Beat me by a minute.
    – Erik
    Dec 17 '20 at 14:07
  • Wouldn't 'yyyy/MM/dd' give "2020/07/15" rather than "2020-07-15", which would need "yyyy-MM-dd"?
    – Dave X
    Dec 17 '20 at 18:22
  • If it's a Date field, the result will be "2020-07-15", QGIS automatically stores with the separator "-" and you can't overwrite it. But if your field is a text field, then the result will be "2020/07/15". Dec 18 '20 at 7:43
2

Maybe try this:

to_date(substr("EndDate",1,10)

edit: Ah, Vincent Bré was a bit faster apparently, and his solution is probably cleaner as well, but this one works also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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