简体   繁体   English

将十进制 DAYS 转换为 TIME 格式

[英]CONVERT decimal DAYS to TIME format

Hello iam a newbie in sql and i need your help.你好,我是 sql 的新手,我需要你的帮助。

I have the following table:我有下表:

    start_date      |     end_date          
02.08.2012 09:27:19 |  06.08.2012 07:53:00  
06.08.2012 06:58:58 |  06.08.2012 13:42:33
05.06.2012 14:35:58 |  05.06.2012 14:42:31

I need to display the duration between start_date and end_date.我需要显示 start_date 和 end_date 之间的持续时间。

I did it like this: end_date - start_date = duration我是这样做的: end_date - start_date = duration
but it displays a decimal value (see below table - field duration).但它显示一个十进制值(见下表 - 字段持续时间)。

I need field duration to display like this: HH:MM:SS - not decimal.我需要像这样显示字段持续时间:HH:MM:SS - 不是十进制。

    start_date      |     end_date         |         duration        | I need it like this
02.08.2012 09:27:19 |  06.08.2012 07:53:00 | --> 3.93450231481481    |    94:25:41
06.08.2012 06:58:58 |  06.08.2012 13:42:33 | --> 0.280266203703704   |    06:43:35
05.06.2012 14:35:58 |  05.06.2012 14:42:31 | --> 0.0045486111111...  |    00:06:33

If something is uncertain i will try to explain.如果有什么不确定的,我会尝试解释。 I hope you can help me.我希望你能帮助我。 Have a nice day.有一个美好的一天。

The subtraction of the dates gives you the number of days.日期的减法为您提供天数。 You can turn them into an INTERVAL DAY TO SECOND value using the NumToDSInterval function.您可以使用NumToDSInterval函数将它们转换为INTERVAL DAY TO SECOND值。

With start date 02.08.2012 09:27:19 and end date 06.08.2012 07:53:00 , the result is close but not quite what you want (and notice the float-type rounding thing where 41 seconds becomes 40.9999999...):开始日期为02.08.2012 09:27:19和结束日期为06.08.2012 07:53:00 ,结果很接近但不是你想要的(并注意浮点型舍入,其中 41 秒变为 40.9999999 ... ):

SQL> SELECT NumToDSInterval(
  2    TO_DATE('06.08.2012 07:53:00', 'DD.MM.YYYY HH24:MI:SS') -
  3    TO_DATE('02.08.2012 09:27:19', 'DD.MM.YYYY HH24:MI:SS'), 'DAY') AS Elapsed
  4  FROM DUAL;

ELAPSED
-----------------------------
+000000003 22:25:40.999999999

But it's a good starting point because once the elapsed time is in an INTERVAL type you can EXTRACT days, hours, minutes and seconds.但这是一个很好的起点,因为一旦经过时间处于INTERVAL类型,您就可以EXTRACT天数、小时数、分钟数和秒数。 I'd do it something like this:我会这样做:

WITH spans AS (
  SELECT NUMTODSINTERVAL(end_date - start_date, 'DAY') AS Elapsed
  FROM myTable
)
SELECT
  EXTRACT(DAY FROM Elapsed) * 24 + EXTRACT(HOUR FROM Elapsed) || ':' ||
  EXTRACT(MINUTE FROM Elapsed) || ':' ||
  ROUND(EXTRACT(SECOND FROM Elapsed), 0) AS duration
FROM spans

I tried this with your first set of dates and it worked just fine;我用你的第一组日期试过这个,效果很好; the ROUND made the seconds come out correctly as 41. ROUND使秒数正确显示为 41。


Addendum OP needs to use this logic in a view, and I'm pretty sure a CTE (Common Table Expression, otherwise know as "WITH foo AS (query)") won't fly for a view.附录OP 需要在视图中使用此逻辑,我很确定 CTE(公用表表达式,否则称为“WITH foo AS(查询)”)不会为视图而飞行。

To use this in a view, move the CTE to a subquery instead:要在视图中使用它,请将 CTE 移至子查询:

CREATE OR REPLACE VIEW myView AS
  SELECT
    EXTRACT(DAY FROM Elapsed) * 24 + EXTRACT(HOUR FROM Elapsed) || ':' ||
    EXTRACT(MINUTE FROM Elapsed) || ':' ||
    ROUND(EXTRACT(SECOND FROM Elapsed), 0) AS duration
  FROM (
    SELECT NUMTODSINTERVAL(end_date - start_date, 'DAY') AS Elapsed
    FROM myTable
  )

I notice that you want hours exceeding one day.我注意到您想要超过一天的小时数。 The following gives you days, hours, minutes, seconds, and works up to 31 days.以下为您提供天数、小时数、分钟数、秒数,最多可工作 31 天。 The idea is to convert this to a date and then use to_char() for the conversion:这个想法是将其转换为日期,然后使用to_char()进行转换:

select to_char(cast('01-Jan-2000' as date) + duration,
               'dd hh:mi:ss')

Not ideal, but quick.不理想,但很快。

I think you need to use a built in oracle function.我认为您需要使用内置的 oracle 函数。 Try the Date SUB as seen here Oracle Docs / MySQL试试这里的日期 SUB Oracle Docs / MySQL

This will allow you to set the precision on the time that is returned.这将允许您设置返回时间的精度。

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

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