如何确定 INTERVAL 的最大分辨率?
如何确定 INTERVAL 值的最大分辨率?例如:
INTERVAL '100 天零 3 秒'
=>日期时间 '20:05' - 时间 '12:01:01'
=>小时AGE(NOW(), NOW() - INTERVAL '1 MONTH')
=>月
How can I determine the largest resolution of an INTERVAL value? For example:
INTERVAL '100 days and 3 seconds'
=> dayTIME '20:05' - TIME '12:01:01'
=> hourAGE(NOW(), NOW() - INTERVAL '1 MONTH')
=> month
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题并不是 100% 清楚,所以答案可能正是你正在寻找的,也可能不是你正在寻找的,但是......
有一个
justify_interval()
函数,您可能想研究一下。因为提取年份,然后是月份,然后是日期等,直到您得出非零答案:
在评论中回复您的其他问题:
The question isn't 100% clear so the answer may or may not be exactly what you're looking for, but...
There is a
justify_interval()
function, which you might want to look into.For there extract the year, then month, then day, etc. until you come up with a non-zero answer:
Re your other question in comments:
INTERVAL
是 12 个字节,并且是包含月、日和微秒的结构,范围为 +/- 178000000 年。由于它存储此信息的方式,它的最大大小始终固定为 178000000 年。请小心理解“一个月”,因为儒略月不是像
小时
或分钟
那样的常量(例如,有多少天)二月?或者一年有多少天?实际上并不总是 30 或 365,PostgreSQL 根据 IRC 上的有趣对话正确更新了内容,添加了 1 个月<。 /code>::到一月的间隔 因为它会增加 struct tm 的 tm_mon 成员(在这种情况下,会回滚到之前的有效日期)。30 号将导致 2 月的最后一天, 您正在寻找给定的 INTERVAL 的最大“非零整数单位”。
我现在得到了这个问题(或者至少我这么认为) 。 我认为在返回该信息的函数中。您将必须链接条件类型和返回类型一些示例
PL
代码:INTERVAL
is 12 bytes and is a struct containing months, days and microseconds and has a range of +/- 178000000 years. It always has a fixed max size of 178000000 years due to the way that it stores this information.Be careful with your understanding of "a month" because the Julian month is not a constant in the same way that an
hour
or aminute
is (e.g. how many days are in the month of February? Or how many days are there in ayear
? It's not always 30 or 365 in reality and PostgreSQL updates things correctly. per an interesting conversation on IRC, Adding1 month
::INTERVAL to January 30th will result in whatever the last day of February because it increments thetm_mon
member ofstruct tm
(and in this case, rolls back to the previous valid date).Ah ha! I get the question now (or at least I think so). You're looking to determine the largest "non-zero integer unit" for a given
INTERVAL
.PostgreSQL doesn't have a built-in function that returns that information. I think you're going to have to chain a conditional and return type. Some example
PL
code: