SQL ORDER BY日期问题 [英] SQL ORDER BY date problem

查看:135
本文介绍了SQL ORDER BY日期问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我解决这个问题吗?我正在尝试按日期排序SQL查询的结果,但我没有得到我需要的结果。



我使用的查询是: p>

  SELECT date FROM tbemp ORDER BY date ASC 

结果是:

  01/02/2009 
03/01/2009
04/06/2009
05/03/2009
06/12/2008
07/02/2009

结果应该是:

  06/12/2008 
03/01/2009
01/02/2009
07/02/2009

我需要以上述格式选择日期。



您的帮助非常感激。

解决方案

date列不是datetime类型,而是varchar。您必须将其转换为日期时间排序:

  select date 
from tbemp
order by convert日期时间,日期,103)ASC

style 103 = dd / MM / yyyy ( msdn


Can you please help me in solving this problem. I am trying to order the results of an SQL query by date, but I'm not getting the results I need.

The query I'm using is:

SELECT date FROM tbemp ORDER BY date ASC

Results are:

01/02/2009
03/01/2009
04/06/2009
05/03/2009
06/12/2008
07/02/2009

Results should be:

06/12/2008
03/01/2009
01/02/2009
07/02/2009

I need to select the date in the format above.

Your help is much appreciated.

解决方案

It seems that your date column is not of type datetime but varchar. You have to convert it to datetime when sorting:

select date
from tbemp
order by convert(datetime, date, 103) ASC

style 103 = dd/MM/yyyy (msdn)

这篇关于SQL ORDER BY日期问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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