当前位置: 代码迷 >> Sql Server >> 奇葩有关问题一个…
  详细解决方案

奇葩有关问题一个…

热度:50   发布时间:2016-04-24 10:16:17.0
奇葩问题一个……求助
sql server2008里边,全代码太长,我简要说说……

DATEDIFF(MINUTE,alarm_time,end_time)/60     现在这个的结果是各种值
但只要在前面加上 XX - ,这个值就变成了2……
比如 700-DATEDIFF(MINUTE,alarm_time,end_time)/60 结果是698。

求真相


------解决方案--------------------
DECLARE @BeginTime DATETIME
DECLARE @EndTime DATETIME
DECLARE @TotalTime DECIMAL(18, 2) 
DECLARE @alarm_time DATETIME    --代表表中alarm_time字段
DECLARE @end_time DATETIME      --代表表中end_time字段
 
SET @BeginTime = '2014/7/1 0:00:00'
SET @EndTime = '2014/07/31 23:59:59'
SET @end_time = '2014/7/29 0:00:00'
SET @alarm_time = '2014/6/30 1:00:00'

SELECT  @TotalTime = CONVERT(DECIMAL(18, 2), ROUND(CONVERT(DECIMAL(18, 2), DATEDIFF(MINUTE,
                                                              @BeginTime,
                                                              @EndTime)) / 60,
                                                   2))
PRINT @TotalTime

SELECT  @totaltime AS a ,
        ROUND(CONVERT(DECIMAL(18, 2), DATEDIFF(MINUTE, @BeginTime, @end_time))
              / 60, 2) AS b ,
        CONVERT(DECIMAL(18, 2), DATEDIFF(MINUTE, @BeginTime, @end_time)) / 60 AS c ,
        DATEDIFF(MINUTE, @BeginTime, @end_time) AS d ,
        CONVERT(DECIMAL(18, 2), ROUND(CONVERT(DECIMAL(18, 2), DATEDIFF(MINUTE,
                                                              @BeginTime,
                                                              @end_time)) / 60,
                                      2)) AS e



SELECT  CASE WHEN @alarm_time < @BeginTime
                  AND @end_time < @EndTime
             THEN @totaltime
                  - CONVERT(DECIMAL(18, 2), ROUND(CONVERT(DECIMAL(18, 2), DATEDIFF(MINUTE,
                                                              @BeginTime,
                                                              @end_time)) / 60,
                                                  2))
             WHEN @end_time > @EndTime
                  AND @alarm_time > @BeginTime
             THEN @totaltime
  相关解决方案