我正在使用DateDiff
函数在SQL Query中进行一些计算。如果DateDiff
值为负数
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
& ClosedOn
的类型为DateTime
。如果ClosedOn
> VendorTicketRaisedOn
然后价值变成了新的。
如何处理此案例以将结果生成为0?
如果Value为负数,则DateDiff应将输出设为0而不是-value
DB:SQL SERVER 2008
答案 0 :(得分: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