按日期选择sql数据顺序 [英] select sql data order by date

查看:114
本文介绍了按日期选择sql数据顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,列有一个名为 date 的列,作为varchar。内容具体格式为2011年3月11日。

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 them correctly ordered by date?

推荐答案

我在这里盲目地假定MySQL,因为在这个问题的上下文中是有道理的。没有人使用另一个数据库引擎将首先创建此问题。

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 来拯救!给定格式为2000年3月1日,以下转换应该可以工作。

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')

您可能需要调整格式字符串有点。

这篇关于按日期选择sql数据顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆