-1

As title.

There is a way to convert a date from YYYY-M-DD to YYYY-MM-DD?

In db I have 2021-1-01 but I need to get 2021-01-01.

TY

EDIT

I have a value 2021-1-01 and i need to insert it in a db like date 2021-01-01, maybe before I not was clear.

JuConte
  • 513
  • 2
  • 7
  • 18
  • Does this answer your question? [SQL date format convert? \[dd.mm.yy to YYYY-MM-DD\]](https://stackoverflow.com/questions/18597051/sql-date-format-convert-dd-mm-yy-to-yyyy-mm-dd) – GSerg Mar 12 '21 at 11:48
  • Are you saying that you have `2021-1-01` in a variable (PHP maybe) and you want to convert it into a date that will work in a MySQL DATE type column? – RiggsFolly Mar 12 '21 at 12:21
  • **Where** do you have the date? Are you using a programing language? – RiggsFolly Mar 12 '21 at 12:30
  • This value become from a XML file. In tag I have 2021-1-01 and I need to import this value in SQL – JuConte Mar 12 '21 at 15:49

1 Answers1

2

If you have '2021-1-01', then you do not have a date. You have a string. That is a problem with your data model. You should fix the data to store dates using appropriate types -- which are not strings.

MySQL is pretty smart about converting dates, so you can just use:

select date(string_as_date_col)

You can change the type of the column in the table using:

alter table t modify column string_as_date_column date

Here is a db<>fiddle.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • maybe I not explain well. My value is 2021-1-01 and I need to upload it as a date format in a SQL db. – JuConte Mar 12 '21 at 11:54