2017-05-15 64 views
0

上的asp.net应用程序运行的网格图,它我想仅某些小时某些列之间显示,SQL选择,如果时间范围内显示特定的小时某些列

`IF ((datepart(hh, getdate())) between 5 and 18 
    SELECT 
    ,[Hour5],[Hour6],[Hour7],[Hour8],[Hour9],[Hour10],[Hour11],[Hour12], 
    [Hour13],[Hour14],[Hour15],[Hour16],[Hour17],[Hour18] 

    FROM [DB]  

    IF ((datepart(hh, getdate())) between 18 and 5 
    SELECT 
     ,[Hour18],[Hour19],[Hour20],[Hour21],[Hour22],[Hour23],[Hour0], 
[Hour1],[Hour2],[Hour3],[Hour4],[Hour5] 
FROM [DB]  ` 

回答

0

表达​​将始终是虚假的,因为它相当于datepart(hh, getdate()) >= 18 and datepart(hh, getdate()) <= 5。您可以尝试将其重写为datepart(hh, getdate()) <= 5 or datepart(hh, getdate()) >= 18 -note or而不是前面示例中的and

但是,我怀疑你在这里也有另一个逻辑错误。现在,如果getdate()介于05:00:00和05:59:59之间,或者getdate()介于18:00:00和18:59:59之间,那么两个的谓词都会得到满足。这是你的意图吗?如果没有,你可以考虑用这种方式构建查询:

if datepart(hh, getdate()) between 5 and 18 
    select /*stuff*/; 
else 
    select /*other stuff*/; 
+0

太感谢你了,我可以在逻辑错误工作,但如果语法错了我不能:) – MvdM

相关问题