什么是ms Sql查询到oracle的翻译

时间:2018-01-22 07:18:45

标签: oracle database-migration datediff

sql查询是:

Declare @Calculation DECIMAL=ABS(DATEDIFF(SECOND,@NowTime,@ShiftStartTime))/DATEDIFF(SECOND,'00:00:00.0000',@TaktTime)

这里NowTime,ShiftStartTime和TaktTime是时间戳变量,计算是数字格式.. 提前谢谢。

2 个答案:

答案 0 :(得分:0)

SQL> declare
  2    NowTime        date := to_date('2010-11-18 23:45:12', 'yyyy-mm-dd hh24:mi:ss');
  3    ShiftStartTime date := to_date('2010-11-18 22:44:40', 'yyyy-mm-dd hh24:mi:ss');
  4    TaktTime       date := to_date('2020-01-01 00:03:11', 'yyyy-mm-dd hh24:mi:ss');
  5  begin
  6    dbms_output.put_line('Result = ' || (TaktTime - trunc(TaktTime)) / (NowTime - ShiftStartTime));
  7  end;
  8  /

Result = ,05258810572687224669603524229074

答案 1 :(得分:0)

Oracle没有TIME数据类型 - 它具有DATE(精确到秒的整数秒)和TIMESTAMP(9)(达到纳秒精度的天数)。如果您要使用TIME(7)和小数秒,则需要使用TIMESTAMP(7),然后使用Oracle的INTERVAL DAY TO SECOND数据类型计算差异:

DECLARE
  NowTime        TIMESTAMP(7) := SYSTIMESTAMP; -- TIMESTAMP '2018-01-22 13:30:00.0000000';
  ShiftStartTime TIMESTAMP(7) := TIMESTAMP '2018-01-22 06:00:00.0000000';
  TaktTime       TIMESTAMP(7) := TIMESTAMP '2018-01-22 14:30:00.0000000';

  ShiftLength    INTERVAL DAY(3) TO SECOND(7) := NowTime - ShiftStartTime;
  TaktLength     INTERVAL DAY(3) TO SECOND(7) := TaktTime - TRUNC( TaktTime );

  Value NUMBER;
BEGIN
  Value := ABS(
             (
               EXTRACT( DAY    FROM ShiftLength ) * 24 * 60 * 60
               EXTRACT( HOUR   FROM ShiftLength )      * 60 * 60
               EXTRACT( MINUTE FROM ShiftLength )           * 60
               EXTRACT( SECOND FROM ShiftLength )
             )
             /
             (
               EXTRACT( DAY    FROM TaktLength ) * 24 * 60 * 60
               EXTRACT( HOUR   FROM TaktLength )      * 60 * 60
               EXTRACT( MINUTE FROM TaktLength )           * 60
               EXTRACT( SECOND FROM TaktLength )
             )
           );
END;
/