akansha
akansha

Reputation: 1

Convert dd.mm.yyyy to yyyy-mm-dd in SQL

I need to convert a date in format dd.mm.yyyy to yyyy-mm-dd For example, if the date is 04.12.2020, i want it to be like 2020-12.04(which is 4th december) But the command giving me the output as 2020-04-12( which is 12th April) select convert(date,'04.12.2020' )

Upvotes: 0

Views: 164

Answers (2)

Gabriel Grünberg
Gabriel Grünberg

Reputation: 36

It sounds like you have a date as a string? (ie. the "04.12.2020"). MySql has a STR_TO_DATE() function:

select str_to_date('04.12.2020', '%d.%m.%Y')

That should convert it to an actual date datatype.

If needed an actual date can then be formated in which ever way you want or just visualized depending on what you're trying to do with DATE_FORMAT():

select date_format(date, '%Y-%m-%d')

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269933

I think you just want str_to_date():

select str_to_date('04.12.2020', '%d.%m.%Y')

Upvotes: 1

Related Questions