pg_sql关于时间的函数

1、时间戳和日期之间的相互转换

时间戳转日期(时间戳为数值类型,若为字符型需进行转换)

复制代码
# 保留到秒:2025-10-02 04:46:40 (字符型转换数值型)
select to_timestamp('1759351600'::bigint)

# 保留到日:2025-10-02
select date(to_timestamp(1759351600))

# 自定义格式:2025/10/02
to_char(to_timestamp(1759351600),'YYYY/mm/dd')

2、interval

作用:实现时间日期的加减

格式:日期 +/- interval '加减值'

复制代码
# 前一天
select current_date - interval '1 day'

# 后一周
select current_date + interval '1 week'

# 前一月
select current_date - interval '1 month'

# 后一年
select current_date + interval '1 year'

3、date_trunc

作用:获取日期对应参数的起始

格式:date_trunc(('参数',日期))

复制代码
# 获取年第一天
select date_trunc('year',now())

# 获取年最后一天(获取下一年的第一天后减一天)
select date_trunc('year',now() + interval '1 year') - interval '1 day'

# 获取季第一天
select date_trunc('quarter',now())

# 获取季最后一天
select date_trunc('quarter',now() + interval '3 month') - interval '1 day'

# 获取月第一天
select date_trunc('month',now())

# 获取月最后一天
select date_trunc('month',now() + interval '1 month') - interval '1 day'

# 获取周第一天
select date_trunc('week',now())

# 获取周最后一天
select date_trunc('week',now() + interval '1 week') - interval '1 day'

计算类:

剩余时间

复制代码
# 周
select date_trunc('week',now() + interval '1 week') - now()

# 月
select date_trunc('month',now() + interval '1 month') - now()

# 季
select date_trunc('quarter',now() + interval '3 month') - now()

# 年
select date_trunc('year',now() + interval '1 year') - now()

剩余天数

复制代码
# 周
select date_trunc('week',current_date + interval '1 week') - interval '1 day' - current_date 

# 月
select date_trunc('month',current_date  + interval '1 month') - interval '1 day' - current_date 

# 季
select date_trunc('quarter',current_date  + interval '3 month') - interval '1 day' - current_date 

# 年
select date_trunc('year',current_date  + interval '1 year') - interval '1 day' - current_date 

4、date_part

作用:获取日期对应的参数是当前年的第几(周、月、季)

格式:date_part('参数',日期)

复制代码
# 获取当前时间是第几天
select date_part('day',now())

# 获取当前时间是第几周
select date_part('week',now())

# 获取当前时间是第几月
select date_part('month',now())

# 获取当前时间是第几季
select date_part('quarter',now())

5.extract

作用:提取时间中某些内容(可以实现和date_part类似功能)

格式:extract(提取值from时间)

复制代码
# 天
select extract(day from current_date)

# 月
select extract(month from current_date)

# 年
select extract(year from current_date)

# 周
select extract(week from current_date)

# 季
select extract(quarter from current_date)
相关推荐
十二同学啊4 小时前
向量数据库:从核心原理到 RAG 与 Java 实战
数据库
蓝速科技5 小时前
会议室门牌公告通知发布选型与落地指南丨蓝速科技
大数据·运维·数据库·人工智能·科技
一 乐7 小时前
二手交易平台|基于springboot + vue二手交易平台(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序
SelectDB8 小时前
同等资源下 Apache Doris 4.2 vs StarRocks 4.1.1:1TB SSB 与 TPC-H 性能实测
大数据·数据库·数据分析
这个DBA有点耶8 小时前
数据库集群与分布式架构:三条技术路线对比、金仓KES RAC实测数据与决策框架
数据库·程序员·架构
MayBaymax8 小时前
MongoDB 索引与事务
java·数据库·mongodb
SelectDB8 小时前
Doris vs ClickHouse:企业 OLAP 走向下一阶段,两种技术路线如何选择
大数据·数据库·数据分析
vx-程序开发8 小时前
【计算机毕设】django校园跑腿服务系统83141
java·数据库·vue.js·spring boot·spring·elasticsearch·django
一 乐9 小时前
养老院管理系统|基于springboot + vue养老院管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·毕业设计
这个DBA有点耶10 小时前
InnoDB页结构深入:页分裂、页合并、填充因子——B+树底层机制全解析
数据库·mysql·架构