2013-05-15 60 views
0

我正在使用DateDiff函數在SQL查詢中進行一些計算。我計算出了問題,如果DateDiff價值排在負DateDiff在sql中返回負值

Declare @ActualTime Numeric(10,2)=677.00 

Select Convert(Numeric(10,2),(100*((@ActualTime-(Convert(Numeric(10,2),CONVERT(VARCHAR(10),SUM(DATEDIFF(Minute,VendorTicketRaisedOn ,ClosedOn))/60)+'.'+ 
     Right('00'+CONVERT(VARCHAR(2),SUM(DATEDIFF(Minute, VendorTicketRaisedOn , ClosedOn)) % 60),2))))/@ActualTime*1)))[AGS Uptime %] 
     From Ticket 

兩列VendorTicketRaisedOn & ClosedOnDateTime類型。如果ClosedOn>VendorTicketRaisedOn然後價值來到negetive。

如何處理這種情況下產生結果爲0?

如果值是負則DateDiff的應該給輸出0而不是 - 值

DB:SQL SERVER 2008

+0

你能解釋一下你的compution一個位? – Serge

回答

1

查詢:

Declare @ActualTime Numeric(10,2)=677.00 

SELECT CASE WHEN ClosedOn > VendorTicketRaisedOn 
      THEN 0 
     ELSE Convert(Numeric(10,2),(100*((@ActualTime-(Convert(Numeric(10,2),CONVERT(VARCHAR(10),SUM(DATEDIFF(MINUTE,VendorTicketRaisedOn ,ClosedOn))/60)+'.'+ Right('00'+CONVERT(VARCHAR(2),SUM(DATEDIFF(MINUTE, VendorTicketRaisedOn , ClosedOn)) % 60),2))))/@ActualTime*1))) 
     END AS [AGS Uptime %] 
FROM Ticket