从日期差将00:45:00转换为0.45或01:45:00转换为1.45?

时间:2018-11-04 12:15:54

标签: sql sql-server

我该如何从日期差将00:45:00转换为0.45或将01:45:00转换为1.45。我在下面的查询中使用

array (
  2130701433 => 
  array (
    'NAME' => 
    array (
      0 => 'aaa',
      1 => 'bbb',
      2 => 'ccc',
    ),
    'VALUE' => 
    array (
      0 => 'aaa',
      1 => 'bbb',
      2 => 'ccc',
    ),
    'EXPIRE' => 
    array (
      0 => '1 second',
      1 => '2 minute',
      2 => '3 hours',
    ),
    'CREATED' => 
    array (
      0 => 'Y:m:d H:i:s',
      1 => 'Y:m:d H:i:s',
      2 => 'Y:m:d H:i:s',
    ),
  ),
  2130701434 => 
  array (
    'NAME' => 
    array (
      0 => 'aaa',
      1 => 'bbb',
      2 => 'ccc',
    ),
    'VALUE' => 
    array (
      0 => 'aaa',
      1 => 'bbb',
      2 => 'ccc',
    ),
    'EXPIRE' => 
    array (
      0 => '1 second',
      1 => '2 minute',
      2 => '3 hours',
    ),
    'CREATED' => 
    array (
      0 => 'Y:m:d H:i:s',
      1 => 'Y:m:d H:i:s',
      2 => 'Y:m:d H:i:s',
    ),
  ),
  2130701435 => 
  array (
    'NAME' => 
    array (
      0 => 'aaa',
      1 => 'bbb',
      2 => 'ccc',
    ),
    'VALUE' => 
    array (
      0 => 'aaa',
      1 => 'bbb',
      2 => 'ccc',
    ),
    'EXPIRE' => 
    array (
      0 => '1 second',
      1 => '2 minute',
      2 => '3 hours',
    ),
    'CREATED' => 
    array (
      0 => 'Y:m:d H:i:s',
      1 => 'Y:m:d H:i:s',
      2 => 'Y:m:d H:i:s',
    ),
  ),
)

但在无法获得与上述相同的精确结果时

选择select cast(datepart(hour, '01:45:00')+datepart(minute, '01:45:00') / 100.00 as decimal(7,4));

1 个答案:

答案 0 :(得分:3)

第一个答案似乎就是您想要的。

如果将第二个表达式分开,则会发现:

datediff(minute,'00:00:00','01:45:00')

计算结果为105,而不是45。

如果要对日期部分使用算术,只需使用分钟:

cast(datediff(minute,'00:00:00','01:45:00') / 60 +
     (datediff(minute,'00:00:00','01:45:00') % 60 / 100.0
     ) as decimal(7, 4)
    )