首页 > 解决方案 > postgres中excel的hour()函数(等效)

问题描述

我最近正在使用 postgres,我必须进行一些计算。但是我无法模仿Excel的HOUR()函数,我阅读了官方资料但对我没有多大帮助。

该函数接收一个小数并获得相同的小时、分钟和秒,例如十进制 0,99988426 返回 11:59:50。尝试使用 to_timestamp 函数在 postgres(我使用 PostgreSQL 10.4)中执行此操作: select to_char (to_timestamp (0.99988426), 'HH24: MI: SS'); 本次返程时间为 19:00:00。当然我省略了一些东西,关于如何解决这个问题的一些想法?

标签: postgresqlexcel-formulaequivalent

解决方案


24:00:00 or 86400 seconds = 1 
Half day(12:00 noon) or 43200 seconds = 43200/86400 = 0.5
11:59:50 or 86390 seconds = 86390/86400 = 0.99988426

因此,要将十进制值转换为时间,您只需将其与 86400 相乘,这将为您提供秒数并通过以下方式将其转换为您的格式:

SELECT TO_CHAR((0.99988426 * 86400) * '1 second'::interval, 'HH24:MI:SS');

SELECT (0.99988426 * 86400) * interval '1 sec';

推荐阅读