软件下载吧文章资讯

分类分类

教你postgresql 中的如何时间处理小技巧

2024-02-03 17:42作者:下载吧

时间格式处理

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

教你postgresql 中的如何时间处理小技巧

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

教你postgresql 中的如何时间处理小技巧

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

教你postgresql 中的如何时间处理小技巧

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

教你postgresql 中的如何时间处理小技巧

时间运算

加减运算

‘-‘ :前x天/月/年

‘+’ :后x天/月/年

current_timestamp – interval ‘x day/month/year…’ 返回时间戳

教你postgresql 中的如何时间处理小技巧

date_part(‘day’, current_timestamp – time1) 两个时间相差的天数

教你postgresql 中的如何时间处理小技巧

返回时间间隔的秒数

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

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

教你postgresql 中的如何时间处理小技巧

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

教你postgresql 中的如何时间处理小技巧

参考资料:

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

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

到此这篇关于教你postgresql 中的如何时间处理小技巧的文章就介绍到这了,更多相关postgresql 时间处理内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

展开全部

相关文章

说两句网友评论
    我要跟贴
    取消