简体   繁体   English

如何将计算列的数字格式转换为时间(hh:mm:ss)格式,sql 查询

[英]How to convert number format of a calculated column to time (hh:mm:ss) format, sql query

First I'm sorry for the poor English but I'll try to be as understandable as possible.首先,我为糟糕的英语感到抱歉,但我会尽量让人们理解。

I'm trying to make a Oracle Database SQL Query which subtracts the datetime value of two existing columns, I managed how to do it using the instruction shown below but the column TEMPO that I created is returning with NUMBER format.我正在尝试创建一个 Oracle 数据库 SQL 查询,该查询减去两个现有列的日期时间值,我使用下面显示的指令管理了如何执行此操作,但我创建的列 TEMPO 以 NUMBER 格式返回。

SELECT M.NUMOS, M.CODFUNCOS, M.DTINICIOOS, M.DTFIMSEPARACAO,
M.DTFIMSEPARACAO - M.DTINICIOOS AS TEMPO 
FROM PCMOVENDPEND M
WHERE DTFIMSEPARACAO IS NOT NULL
AND DATA >= SYSDATE-1

The Output is at it goes. Output 就是这样。

Output Output

I need to convert the column TEMPO format to TIME as 'hh:mm:ss', and if some results passes through 24 hours it keeps adding like 32:01:20 (just like the [hh]:mm:ss Excel formatting).我需要将列 TEMPO 格式转换为 TIME 作为 'hh:mm:ss',如果某些结果经过 24 小时,它会像 32:01:20 一样不断添加(就像 [hh]:mm:ss Excel 格式) .

I tried a few things like the instruction below but it returns ORA-00932 error inconsistent data type, expected TIME and returned DATE JULIAN (I don't know if it is the exact error description, my DBX is in Portuguese), unfortunately I can't do any changes to the root tables if it would help.我尝试了一些类似下面的指令,但它返回ORA-00932 错误,数据类型不一致,预期 TIME 并返回 DATE JULIAN (我不知道它是否是确切的错误描述,我的 DBX 是葡萄牙语),不幸的是我可以如果有帮助,请不要对根表进行任何更改。

SELECT M.NUMOS, M.CODFUNCOS, M.DTINICIOOS, M.DTFIMSEPARACAO,
CAST(M.DTFIMSEPARACAO - M.DTINICIOOS AS TIME) AS TEMPO 
FROM PCMOVENDPEND M
WHERE DTFIMSEPARACAO IS NOT NULL
AND DATA >= SYSDATE-1

Anyways, it is so important that the column TEMPO format is TIME because I'll use the sql query as an instruction to PowerBi Direct Query and I can't import the database to work it with PowerQuery since it is too large data.无论如何,列 TEMPO 格式为 TIME 非常重要,因为我将使用 sql 查询作为 PowerBi 直接查询的指令,并且我无法导入数据库以使用 PowerQuery,因为它是太大的数据。

Thanks everyone!感谢大家!

A function makes it prettier. function 让它更漂亮。 It returns number of days formatted as dd:hh:mi:ss .它返回格式为dd:hh:mi:ss的天数

SQL> create or replace function f_days2ddhhmiss (par_broj_dana in number)
  2     return varchar2
  3  is
  4     l_broj_dana  number := par_broj_dana;
  5     retval       varchar2 (20);
  6  begin
  7     with podaci
  8          as (select trunc (l_broj_dana) broj_dana,
  9                     round (mod (l_broj_dana * 24, 24), 2) broj_sati,
 10                     round (mod (l_broj_dana * 24 * 60, 60), 2) broj_minuta,
 11                     round (mod (l_broj_dana * 24 * 60 * 60, 60), 2)
 12                        broj_sekundi
 13                from dual)
 14     select    lpad (p.broj_dana, 2, '0')
 15            || ':'
 16            || lpad (trunc (p.broj_sati), 2, '0')
 17            || ':'
 18            || lpad (trunc (p.broj_minuta), 2, '0')
 19            || ':'
 20            || lpad (trunc (p.broj_sekundi), 2, '0')
 21       into retval
 22       from podaci p;
 23
 24     return retval;
 25  end f_days2ddhhmiss;
 26  /

Function created.

Example:例子:

Without it, you get decimal number:没有它,你会得到十进制数:

SQL> select to_date('07.08.2020 14:25', 'dd.mm.yyyy hh24:mi:ss')
  2       - to_date('03.08.2020 13:20', 'dd.mm.yyyy hh24:mi:ss') result
  3  from dual;

    RESULT
----------
4,04513889

With it, you get what you wanted:有了它,你会得到你想要的:

SQL> select f_days2ddhhmiss(to_date('07.08.2020 14:25', 'dd.mm.yyyy hh24:mi:ss')
  2                       - to_date('03.08.2020 13:20', 'dd.mm.yyyy hh24:mi:ss')
  3                        ) result
  4  from dual;

RESULT
--------------------------------------------------------------------------------
04:01:05:00

SQL>

Yes, such (or similar) code can be used directly in SQL, but it makes a SELECT statement kind of ugly and difficult to read.是的,这样的(或类似的)代码可以直接在 SQL 中使用,但它使SELECT语句有点丑陋且难以阅读。

Your query would then be您的查询将是

SELECT m.numos,
       m.codfuncos,
       m.dtinicioos,
       m.dtfimseparacao,
       f_days2ddhhmiss (m.dtfimseparacao - m.dtinicioos) AS tempo
  FROM pcmovendpend m
 WHERE     dtfimseparacao IS NOT NULL
       AND data >= SYSDATE - 1

See if it helps.看看有没有帮助。

With Littlefoots help I managed a way to convert the column to the desired format, just as follows:在 Littlefoots 的帮助下,我设法将列转换为所需的格式,如下所示:

CREATE OR REPLACE FUNCTION WMS_CORINGA(TEMPO NUMBER)
RETURN VARCHAR2 IS
HORA NUMBER;
MINUTO NUMBER(2);
SEGUNDO NUMBER(2);
TEMPOABS NUMBER;
BEGIN
IF TEMPO <> 0 THEN
TEMPOABS := ABS(TEMPO);
HORA := TRUNC(TEMPOABS*24);
MINUTO := TRUNC(((TEMPOABS*24)-HORA) * 60);
SEGUNDO := ((((TEMPOABS*24)-HORA) * 60) - MINUTO) * 60;
RETURN TO_CHAR(HORA, 'FM9999900') || ':'|| TO_CHAR(MINUTO, 'FM00') || ':' || TO_CHAR (SEGUNDO, 'FM00');
ELSE
RETURN '';
END IF;
END;

SELECT  M.NUMOS, M.CODFUNCOS, M.DTINICIOOS, M.DTFIMSEPARACAO,
WMS_CORINGA(M.DTFIMSEPARACAO - M.DTINICIOOS) AS TEMPO_EM_SEPARACAO  
FROM PCMOVENDPEND M
WHERE DTFIMSEPARACAO IS NOT NULL
AND DATA>= SYSDATE-1

But I couldn't make the PowerBI connection, the output is Details: "This native database query isn't currently supported."但我无法建立 PowerBI 连接,output 是详细信息:“当前不支持此本机数据库查询。”

Anyways, learned a lot.总之,学到了很多。

You can cast dates to timestamps before subtracting.您可以在减去之前将日期转换为时间戳。 The result of the subtraction is an interval represented as "days hh24:mi:ss.fff".减法的结果是表示为“days hh24:mi:ss.fff”的间隔 If necessary you can then use the extract function to pull out the individual components.如有必要,您可以使用提取物function 拉出各个组件。 That way you have neither an ugly query nor aa need to create a function.这样,您既不需要丑陋的查询,也不需要创建 function。

with the_dates( dt1, dt2) as 
   ( select to_date('07.08.2020 14:25', 'dd.mm.yyyy hh24:mi:ss')
          , to_date('03.08.2020 13:20', 'dd.mm.yyyy hh24:mi:ss')  
       from dual
   ) 
select dt1, dt2, cast(dt1 as timestamp) - cast(dt2 as timestamp) result
  from the_dates;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM