Dividing HH:MM:SS by an integer

前端 未结 1 1226
面向向阳花
面向向阳花 2021-01-24 02:21

I have a column, Col1, in the format HH:MM:SS, and an integer column Col2. I am trying to compute Col1 / Col2 in the format <

相关标签:
1条回答
  • 2021-01-24 02:26

    Assuming you are using sql server (2008 or higher), perhaps the following will clear things up for you:

    -- starting with a string in HH:MM:SS format
    declare @s varchar(8)
    set @s = '12:00:00'
    
    -- set a divisor
    declare @d int
    set @d = 2
    
    -- divide the total seconds by the divisor
    set @s = convert(time(0), dateadd(second, datediff(second, 0, @s) / @d, 0))
    
    -- output the results   '06:00:00'
    print @s
    

    Note that you really should just keep time values in a time datatype to begin with, but there are implicit conversions allowed that will let you go back and forth like this.

    0 讨论(0)
提交回复
热议问题