postgresql - 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。当然我省略了一些东西,关于如何解决这个问题的一些想法?
解决方案
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';
推荐阅读
- css - 是否可以同时调整图像的亮度和对比度?
- javascript - 如何使用js查找用户是否可以单击html中的给定按钮?
- node.js - 当(npm run watch)失败时,我如何编译 sass 文件
- amazon-web-services - 如何亚马逊 s3 将存储桶中的文件更新为公共访问
- perl - 找不到 DBD/mysql.pm
- raku - Perl 6中的AES加密?
- java - 如何使用 BaseX 从 Java 运行 XQuery?
- angular - 如何限制用户在已经编辑的前端编辑相同的项目
- html - 引导轮播被自定义 css 覆盖并显示所有图像
- c# - ApplicationUser.Username 为 null,即使 ApplicationUserId 不是