2013-12-23 180 views
0

排序按日期明智的报告问题按日期排列明智报告?

select 
    liftingbirds, totalweight, avgweight, dcno, 
    convert(varchar,liftingdate,103) as liftingdate 
from 
    k_LiftingEntryRecords 
where 
    dcno = @dcno 
order by 
    liftingdate desc 

在这里,我没有得到订单明智的日报表...当我删除where条件 dcno = @dcno,然后谈到为了明智的日报表...

select 
    liftingbirds, totalweight, avgweight, dcno, 
    convert(varchar,liftingdate,103) as liftingdate 
from 
    k_LiftingEntryRecords 
order by 
    liftingdate desc 

其他明智的它不来...像

+0

您的liftingdate列类型是datetime? – Moons

+0

是的,liftdate列是日期时间.. – Sambasiva

+0

请添加示例数据和预期输出 –

回答

1

varchar是一个字符串不是日期。 SQL Server中的字符串比较按字母顺序完成,这与按日期数据类型排序非常不同。让我知道如果你明白了区别,否则我会扩大我的答案

+0

order by is date,输出字段是字符串 –

+0

@JoeTaras'order by'对选择的表达式进行操作。他正在通过'liftingdate'进行排序,该''datedate'已被定义为'select'语句中结果集的一部分作为别名。注意'convert'表达式中的'as'。这个表达式的结果是'varchar' – Leo

+0

liftdate太过名字了!我们的OP必须更改名称,您认为更好的可归档吗? –