当前位置:网站首页>postgresql 两个月份,如何计算一共有几个月?
postgresql 两个月份,如何计算一共有几个月?
2022-07-19 09:19:00 【刘贵宾】
例如2020-12 至2022-07
其他答案1:
SELECT EXTRACT(YEAR FROM age) * 12 + EXTRACT(MONTH FROM age) AS months_between
FROM age(TIMESTAMP '2022-07-01', TIMESTAMP '2020-12-01') AS t(age);
如果要多次执行此操作,则可以定义以下函数:
CREATE FUNCTION months_between (t_start TIMESTAMP, t_end TIMESTAMP)
RETURNS INTEGER
AS $$
SELECT
(
12 * EXTRACT('years' FROM a.i) + EXTRACT('months' FROM a.i)
)::INTEGER
FROM (
VALUES (justify_interval($2 - $1))
) AS a (i)
$$
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
这样你就可以
SELECT months_between('2020-01-01', now());
比如你提供的日期:
SELECT months_between('2020-12-01', '2022-07-01');
边栏推荐
- C explanation of message dialog box based on MessageBox class
- Detailed explanation of feign custom configuration
- 巴比特 | 元宇宙每日必读:虚拟人热度减退,有的“出道即巅峰”,围观人群都散了?...
- The new blue ocean "hard sunscreen" can't cover the 100 billion market
- How to deal with repeated consumption of MySQL data read by Flink
- Failed to process package ‘cartographer_ros‘ :
- Vite 入门
- Degradation technology of template and generic programming
- Druid 配置和监控
- Hackers crack gambling website vulnerabilities and "collect wool" 100000 per month
猜你喜欢
Phpcms add multi text attribute field_ Detailed tutorial
Notepad++ software installation tutorial
【2022】【论文笔记】太赫兹波在非磁化等离子体中的——
Before autumn recruitment, you must read these 8 papers on the recommendation system! [attach thesis materials]
NFT game interoperability: technology is not a roadblock
【数据库】概念设计、逻辑设计、关系数据库设计理论
Why psp22 is important to Polkadot ecosystem
How can I use coreldrawx8 for free when the trial expires? cdrX8
Reading the paper: retreating atmosphere revolution for semantic image segmentation
Stm32+bh1750 photosensitive sensor obtains light intensity
随机推荐
Find - b+ tree
Paper reading: linknet: expanding encoder representations foreefficient semantic segmentation
PyG教程(8):计算更高效的稀疏矩阵形式
[HDU 6095] Rikka with Competition
Débogage du Navigateur
【数据库】索引
Output of STL Vector
Unity实战问题-WebGL问题集锦-下篇
供应链攻击日益严重,微软开源 SBOM 生成工具 Salus
Unity practical problems webgl problem collection part 2
Detailed explanation of feign custom configuration
这些年我开源的几个小项目
黑客破解赌博网站漏洞每月“薅羊毛”10万
Wechat applet_ 18. Communication between parent and child components
.NET 7 新增速率限制 (Rate Limiting) 功能,轻松限制请求数量
《强化学习基础》1
浏览器调试
为什么独立站卖家喜欢做电子邮件营销?原来转化率能提高这么多!
Live short video source code - the development sequence of live short video source code is five steps
QIODevice