How to divide a time type variable by 2 in SQL Server

  • A+
Category:Languages

I have a variable of type TIME, I need to divide the time of this variable by 2.

My SQL code:

DECLARE @TEMPO AS TIME = '01:31:00'  SELECT (@TEMPO / 2 ) AS RESULT  --the result should be: 00:45:30 

SQL Server however throws an error:

Operand type clash: time is incompatible with int

Every help is welcome!

 


Just another option is to convert the time to datetime and then to float, and then divide by two.

Example

DECLARE @TEMPO AS TIME = '01:31:00'  Select convert(time,convert(datetime,convert(float,convert(datetime,@Tempo))/2)) 

Returns

00:45:30.0000000 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: