SQL - Operand data type datetime2 is invalid for subtract operator

后端 未结 2 1043
逝去的感伤
逝去的感伤 2021-01-20 02:11

I want to subtract the value of the first row from the value of the second row of column _timestamp (shown below). _number is the ordering column i

2条回答
  •  粉色の甜心
    2021-01-20 02:33

    It's indeed not feasible with the datetime2

    DECLARE @t1 DATETIME2(7) = '2020-01-01 13:20:00.000'
    DECLARE @t2 DATETIME2(7) = '2020-01-01 13:22:23.000'
     
    SELECT  CONVERT(VARCHAR(50),@t2 - @t1, 108)
    
    >> Operand data type datetime2 is invalid for subtract operator.
    

    But when converted to a datetime, it gives a nice result ;-)

    DECLARE @t1 DATETIME2(7) = '2020-01-01 13:20:00.000' 
    DECLARE @t2 DATETIME2(7) = '2020-01-01 13:22:23.000'
    
    SELECT  CONVERT(VARCHAR(50), CONVERT(DATETIME, @t2) - CONVERT(DATETIME, @t1), 108)
    
    >> 00:02:23
    

提交回复
热议问题