select sql data order by date

后端 未结 3 1829
我寻月下人不归
我寻月下人不归 2021-01-26 17:09

I have a table with a column called date, as a varchar. The contents are specifically formatted like \'March 11, 2011\'.

How can I select the results have

3条回答
  •  灰色年华
    2021-01-26 17:25

    I'm blindly assuming MySQL here, because it makes sense in the context of this question. Nobody using another database engine would dare create this problem in the first place.

    STR_TO_DATE to the rescue! Given the format "March 01, 2000", the following conversion should work.

    SELECT STR_TO_DATE(column_name, '%M %d, %Y')
      FROM TableName
     WHERE ...
     ORDER BY STR_TO_DATE(column_name, '%M %d, %Y')
    

    You may need to adjust the format string a bit.

提交回复
热议问题