Format MySQL date (d-m-Y) - (Y-m-d)

1

I am trying to format the date in MySQL but I do not get it, the format of the date is as follows:

08-05-2017 - 18:03

I want to transform it to

2018-11-09 - 20:30

Using

SELECT DATE_FORMAT(STR_TO_DATE(fecha, '%d-%m-%Y - %H:%i'), '%Y-%m-%d - 
%H:%i') WHERE id = 92

Am I using STR_TO_DATE and DATE_FORMAT correctly?

Thanks

Edito: Correct, missing the FROM silly mistake that I did not realize, then to update I used the UPDATE and that's it, thank you very much to everyone

UPDATE tabla SET fecha = DATE_FORMAT(STR_TO_DATE(fecha, '%d-%m-%Y %H:%i'), '%Y-%m-%d - %H:%i') WHERE id = ? 
    
asked by Jesús 10.11.2018 в 22:45
source

2 answers

2

Your query is correct, of course, if:

  • you add the% share FROM
  • avoid that blank space after the script

It is assumed that you will also add an alias to the resulting column.

And, if your column were of type DATE or DATETIME you would not need STR_TO_DATE . If it's type VARCHAR and you can normalize , then do it.

The query should work like this without problems:

SELECT 
    DATE_FORMAT(STR_TO_DATE(fecha, '%d-%m-%Y - %H:%i'), '%Y-%m-%d - %H:%i') mifecha 
FROM miTabla
WHERE id = 92
    
answered by 10.11.2018 / 23:06
source
1

Your query is perfect, except that the FROM of the table is missing, but if you do this test:

SELECT DATE_FORMAT(STR_TO_DATE('08-05-2017 - 18:03', '%d-%m-%Y - %H:%i'), '%Y-%m-%d - %H:%i')

works fine, returns

2017-05-08 - 18:03

Greetings.

    
answered by 10.11.2018 в 23:05