您现在的位置是:网站首页> 编程资料编程资料
postgresql 中的时间处理小技巧(推荐)_PostgreSQL_
2023-05-27
381人已围观
简介 postgresql 中的时间处理小技巧(推荐)_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
到此这篇关于postgresql 中的时间处理小技巧(推荐)的文章就介绍到这了,更多相关postgresql 时间处理内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关内容
- Postgresql限制用户登录错误次数的实例代码_PostgreSQL_
- PostgreSQL用户登录失败自动锁定的处理方案_PostgreSQL_
- Postgresql设置远程访问的方法(需要设置防火墙或者关闭防火墙)_PostgreSQL_
- 详解PostgreSQL提升批量数据导入性能的n种方法_PostgreSQL_
- PostgreSQL数据库中如何保证LIKE语句的效率(推荐)_PostgreSQL_
- postgresql影子用户实践场景分析_PostgreSQL_
- 如何将postgresql数据库表内数据导出为excel格式(推荐)_PostgreSQL_
- 如何将excel表格数据导入postgresql数据库_PostgreSQL_
- postgresql的jsonb数据查询和修改的方法_PostgreSQL_
- PostgreSQL判断字符串是否包含目标字符串的多种方法_PostgreSQL_
