2011-09-12 103 views
1

我使用的是SQL查詢選擇包含科拉姆值> 1000

select 
    datediff(hour,Convert(DateTime,Convert(DateTime, Convert(nVarChar, cast(complaintdate as datetime), 101)) + '' + 
    Convert(varchar,complainttime,114)),dateadd(mi,330,GETUTCDATE())) 
from complaintregister 

它創建類似

No column name 
    15455 
    15259 
    15190 
    15190 
    15165 
    300 
    200 
    1000 
    500 

一欄,我想一列方含值> 1000

+0

什麼c#和asp標籤在這裏做? – Reniuz

回答

5

只是重複表達條件:

select datediff(hour,Convert(DateTime,Convert(DateTime, Convert(nVarChar, cast(complaintdate as datetime), 101)) +''+ Convert(varchar,complainttime,114)),dateadd(mi,330,GETUTCDATE())) 
from complaintregister 
where datediff(hour,Convert(DateTime,Convert(DateTime, Convert(nVarChar, cast(complaintdate as datetime), 101)) +''+ Convert(varchar,complainttime,114)),dateadd(mi,330,GETUTCDATE())) > 1000 

查詢計劃員應該認識到表達式是相同的,實際上只爲每個記錄計算一次。

或者您可以使用嵌套查詢:

select time 
from (
    select datediff(hour,Convert(DateTime,Convert(DateTime, Convert(nVarChar, cast(complaintdate as datetime), 101)) +''+ Convert(varchar,complainttime,114)),dateadd(mi,330,GETUTCDATE())) as time 
    from complaintregister 
) x 
where time > 1000 
0

如果是SQL服務器,你可以點這樣的事情

select tablename.colname from 
(select datediff(hour,Convert(DateTime,Convert(DateTime, Convert(nVarChar, cast(complaintdate as datetime), 101)) +''+ Convert(varchar,complainttime,114)),dateadd(mi,330,GETUTCDATE())) as colname 
from complaintregister) as tablename 
where tablename.colname > 1000 

還沒有測試數據庫自動取款機,讓你擁有玩弄它有點