按日期SQL服务器排序 [英] Sorting by date SQL server
问题描述
我有一个包含名为Event Date的事件的表格。
我想订购它以便最新的事件日期(接近当前系统日期)位于顶部,其余的如下在下面,然后一旦该日期过去,它应该移动到底部与其余的。
我不知道是否有办法做我想要的但我已经搜索了互联网,我找到了一些可能正在寻找的东西,但它并没有完全正常工作..
以下是一些例子
SELECT * FROM events
< span class =code-keyword> ORDER BY EVENTDATE ASC ,
CASE EVENTDATE WHEN ' future' 那么 EVENTDATE END ASC ,
CASE WHEN EVENTDATE<> ' future' 那么 EVENTDATE END DESC
这个给了我一个错误:从字符串转换日期和/或时间时转换失败。
然后我尝试了这个:
< pre lang =sql> SELECT *
FROM mtc3_doradosbc.cw_events
< span class =code-keyword> ORDER BY
CASE WHEN EVENTDATE> GETDATE()。现在那么 1
WHEN EVENTDATE< GETDATE()那么 2
END ASC ,
EVENTDATE
这个
SELECT *,EVENTDATE( date ,GETDATE()) FROM mtc3_doradosbc.cw_events
order by CASE WHEN mtc3_doradosbc.cw_events< 0 那么 1 ELSE 0 END ,diff
< br $> b $ b
如果有人可以提供帮助我会非常感激
谢谢
示例(这就是我所拥有的)
活动1 ---- 2014年3月10日
活动2 ---- 3月28日2014
活动25 ---- 2014年3月25日
我想要在2014年3月25日
活动25 ---- 2014年3月25日
活动2 ---- 2014年3月28日
活动1 ---- 2014年3月10日
我想在2014年3月26日
活动2 ---- 2014年3月28日
活动25 ---- 2014年3月25日
事件1 ---- 2014年3月10日
第二段代码中可能出现错误。
你可以试试这个
SELECT *
来自mtc3_d oradosbc.cw_events
ORDER BY
CASE EVENDATE> = GETDATE()那么1
当EVENTDATE< GETDATE()那么2
结束ASC,
EVENTDATE ASC
我认为你在寻找 DATEDIFF函数 [ ^ ]。
SELECT 事件,EventDate
FROM EventTable
ORDER BY DATEDIFF(d,EventDate,GETDATE()) ASC 跨度>
I have a table with events with a column called Event Date.
I want to order it so that the Latest event date (closes to the current system date) is on top with the rest following underneath and then as soon as that date passed it should move to the bottom with the rest.
I don't know if there is a way to do what I want but I have searched the internet and I found a few things that might be what I am looking for but its not exactly working..
Here are some examples
SELECT * FROM events
ORDER BY EVENTDATE ASC,
CASE EVENTDATE WHEN 'future' THEN EVENTDATE END ASC,
CASE WHEN EVENTDATE <> 'future' THEN EVENTDATE END DESC
This one gave me an error : Conversion failed when converting date and/or time from character string.
Then I tried this:
SELECT *
FROM mtc3_doradosbc.cw_events
ORDER BY
CASE WHEN EVENTDATE > GETDATE().now THEN 1
WHEN EVENTDATE < GETDATE() THEN 2
END ASC,
EVENTDATE
and this
SELECT *, EVENTDATE(date, GETDATE()) FROM mtc3_doradosbc.cw_events
order by CASE WHEN mtc3_doradosbc.cw_events < 0 THEN 1 ELSE 0 END, diff
If any one could assist i would appreciate it greatly
Thank you
Example ( this is what i have)
Event 1 ----10 Mar 2014
Event 2 ----28 mar 2014
Event 25----25 Mar 2014
I want on the 25 Mar 2014
Event 25----25 Mar 2014
Event 2 ----28 mar 2014
Event 1 ----10 Mar 2014
I want on the 26 Mar 2014
Event 2 ----28 mar 2014
Event 25----25 Mar 2014
Event 1 ----10 Mar 2014
there was a possible miskate in the second block of code.
you can try this
SELECT *
FROM mtc3_doradosbc.cw_events
ORDER BY
CASE WHEN EVENTDATE >= GETDATE() THEN 1
WHEN EVENTDATE < GETDATE() THEN 2
END ASC,
EVENTDATE ASC
I think you're looking for DATEDIFF function[^].
SELECT Event, EventDate FROM EventTable ORDER BY DATEDIFF(d, EventDate, GETDATE()) ASC
这篇关于按日期SQL服务器排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!