2017-05-15 62 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

相關問題