postgresql 时间处理 postgresql 中的时间处理小技巧(推荐)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

postgresql 时间处理 postgresql 中的时间处理小技巧(推荐)

幺零二肆   2021-03-29 我要评论
想了解postgresql 中的时间处理小技巧(推荐)的相关内容吗,幺零二肆在本文为您仔细讲解postgresql 时间处理的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:postgresql,时间处理,下面大家一起来学习吧。

时间格式处理

按照给定格式返回:to_char(timestamp,format)

返回相差的天数:(date(time1) - current_date)

返回时间戳对应的的日期[yyyy-MM-dd]:date(timestamp)

计算结果取两位小数(方便条件筛选):round((ABS(a-b)::numeric / a), 2) * 100 < 10

时间运算

加减运算

'-' :前x天/月/年

'+' :后x天/月/年

current_timestamp - interval 'x day/month/year...' 返回时间戳

date_part('day', current_timestamp - time1) 两个时间相差的天数

返回时间间隔的秒数

两个timestamp 直接相减返回的是 interval类型,而不是毫秒数

extract(epoch from (time1- time2)) * 1000

如果在sql 中使用long类型的 timestamp,需要包裹 to_timestamp() 函数

参考资料:

1. https://www.yiibai.com/manual/postgresql/functions-formatting.html

2. http://www.postgres.cn/docs/9.4/functions-datetime.html

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们