如何使用 postgres 将时间间隔转换为小时数?

发布于 2024-07-24 09:39:44 字数 213 浏览 11 评论 0原文

间隔

4 days 10:00:00

假设我有一个像postgres 中的 。 我如何将其转换为小时数(在本例中为 106?)是否有函数或者我应该硬着头皮做类似的事情

extract(days, my_interval) * 24 + extract(hours, my_interval)

Say I have an interval like

4 days 10:00:00

in postgres. How do I convert that to a number of hours (106 in this case?) Is there a function or should I bite the bullet and do something like

extract(days, my_interval) * 24 + extract(hours, my_interval)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

岁月蹉跎了容颜 2024-07-31 09:39:44

可能最简单的方法是:

SELECT EXTRACT(epoch FROM my_interval)/3600

Probably the easiest way is:

SELECT EXTRACT(epoch FROM my_interval)/3600
养猫人 2024-07-31 09:39:44

如果你想要整数,即天数:

SELECT (EXTRACT(epoch FROM (SELECT (NOW() - '2014-08-02 08:10:56')))/86400)::int

If you want integer i.e. number of days:

SELECT (EXTRACT(epoch FROM (SELECT (NOW() - '2014-08-02 08:10:56')))/86400)::int
盛夏尉蓝 2024-07-31 09:39:44

要获取天数,最简单的方法是:

SELECT EXTRACT(DAY FROM NOW() - '2014-08-02 08:10:56');

据我所知,它会返回与以下内容相同的结果:

SELECT (EXTRACT(epoch FROM (SELECT (NOW() - '2014-08-02 08:10:56')))/86400)::int;

To get the number of days the easiest way would be:

SELECT EXTRACT(DAY FROM NOW() - '2014-08-02 08:10:56');

As far as I know it would return the same as:

SELECT (EXTRACT(epoch FROM (SELECT (NOW() - '2014-08-02 08:10:56')))/86400)::int;
孤独患者 2024-07-31 09:39:44
select floor((date_part('epoch', order_time - '2016-09-05 00:00:00') / 3600)), count(*)
from od_a_week
group by floor((date_part('epoch', order_time - '2016-09-05 00:00:00') / 3600));

::int 转换遵循舍入原则。
如果您想要不同的结果(例如向下舍入),可以使用相应的数学函数,例如 floor

select floor((date_part('epoch', order_time - '2016-09-05 00:00:00') / 3600)), count(*)
from od_a_week
group by floor((date_part('epoch', order_time - '2016-09-05 00:00:00') / 3600));

The ::int conversion follows the principle of rounding.
If you want a different result such as rounding down, you can use the corresponding math function such as floor.

帅哥哥的热头脑 2024-07-31 09:39:44

如果转换表字段:

  1. 定义字段,使其包含秒:

     如果不存在则创建表测试 ( 
           ... 
           字段 间隔秒 (0) 
       ); 
      
  2. 提取值。 记住要转换为 int 否则,一旦间隔很大,你可能会得到一个不愉快的惊喜:

    EXTRACT(EPOCH FROM field)::int

If you convert table field:

  1. Define the field so it contains seconds:

     CREATE TABLE IF NOT EXISTS test (
         ...
         field        INTERVAL SECOND(0)
     );
    
  2. Extract the value. Remember to cast to int other wise you can get an unpleasant surprise once the intervals are big:

    EXTRACT(EPOCH FROM field)::int

冰葑 2024-07-31 09:39:44

如果您想在添加间隔后仅以日期类型显示结果,则应尝试此

Select (current_date +interval 'x day')::date;

If you want to display your result only in date type after adding the interval then, should try this

Select (current_date + interval 'x day')::date;

定格我的天空 2024-07-31 09:39:44

避免到处出现冗长的提取纪元的函数。 返回秒数,因此小时需要 int_interval('1 day') / 3600

CREATE OR REPLACE FUNCTION int_interval(interval_text text) returns INT LANGUAGE SQL IMMUTABLE as $ SELECT EXTRACT(epoch FROM interval_text::INTERVAL)::INT $;

int_interval('1 day')

A function to avoid having verbose extract epoch all over the place. Returns seconds so for hours needs int_interval('1 day') / 3600

CREATE OR REPLACE FUNCTION int_interval(interval_text text) returns INT LANGUAGE SQL IMMUTABLE as $ SELECT EXTRACT(epoch FROM interval_text::INTERVAL)::INT $;

Usage

int_interval('1 day')
初熏 2024-07-31 09:39:44

我正在使用 PostgreSQL 11,我创建了一个函数来获取 2 个不同时间戳之间的小时数

create function analysis.calcHours(datetime1 timestamp, datetime2 timestamp)
    returns integer
    language plpgsql as $
    declare
        diff interval;
    begin
        diff = datetime2 - datetime1;
        return (abs(extract(days from diff))*24 + abs(extract(hours from diff)))::integer;
    end; $;

I'm working with PostgreSQL 11, and I created a function to get the hours betweeen 2 differents timestamps

create function analysis.calcHours(datetime1 timestamp, datetime2 timestamp)
    returns integer
    language plpgsql as $
    declare
        diff interval;
    begin
        diff = datetime2 - datetime1;
        return (abs(extract(days from diff))*24 + abs(extract(hours from diff)))::integer;
    end; $;
遗心遗梦遗幸福 2024-07-31 09:39:44
         select date 'now()' - date '1955-12-15';

这是计算总天数的简单查询。

         select date 'now()' - date '1955-12-15';

Here is the simple query which calculates total no of days.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文