【问题标题】:add number filed to date filed in oracle在 oracle 中将数字字段添加到日期字段
【发布时间】:2021-04-03 03:59:47
【问题描述】:

我正在尝试将秒数添加到现有日期以查找结束时间。我有一个领域的开始时间和一个领域的总持续时间,我在互联网上发现我们可以添加数字到日期,但我们应该在 86400 上潜水

(chargestarttime + (NVL (callduration, 0) / 86400))
// where chargestarttime is date , callduration is number 
// sample (chargestarttime  is 2020-05-30 02:21:58 and callduration is 65 the output is 2020-05-30 02:23:03) 

我的问题是为什么我们需要在 86400 上进行 div 操作?如果我将日期转换为 UNIX_TIMESTAMP,然后仅添加 65,它将在 86400(24 小时)上不使用 div 给出相同的结果,或者我们有理由在 86400(24 小时)上进行 div

【问题讨论】:

    标签: sql oracle datetime date-arithmetic


    【解决方案1】:

    您的查询将秒数添加为一整天的一小部分(即 1 天 = 24 小时 = 1440 分钟 = 86400 秒)。

    或者,您可以使用 INTERVAL 数据类型将秒数直接添加到日期:

    chargestarttime + NVL(callduration, 0) * INTERVAL '1' SECOND
    

    chargestarttime + NUMTODSINTERVAL( NVL(callduration, 0), 'SECOND' )
    

    【讨论】:

      【解决方案2】:

      为什么我们需要在 86400 上进行 div 操作?

      在日期算术中,Oracle 使用1 表示 1 天。如果你从几秒开始,你需要把它转换成天,所以

       chargestarttime + NVL(callduration, 0) / 60 / 60 / 24
                     -- seconds per minutes-----^
                            -- minutes per hour -----^
                                    -- hours per day -----^
      

      86400 只是一天中的秒数。

      【讨论】:

        猜你喜欢
        • 2020-11-22
        • 2013-01-28
        • 2015-07-06
        • 1970-01-01
        • 1970-01-01
        • 2021-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多