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)
相关推荐
Flynt2 小时前
Postgres 19 实测:表膨胀清理,VACUUM FULL 锁到读写归零,REPACK 没停一秒
数据库·postgresql·性能优化
琳少_5 小时前
java程序设计笔记
java·开发语言·数据库·笔记
SelectDB8 小时前
Apache Doris 5.0 IVM 技术解析:物化视图增量刷新原理
数据库
飞奔的瓜牛10 小时前
银河麒麟 V10 SP(x86_64)系统下 MongoDB 社区版 8.0 单机部署实战
数据库·mongodb
Yeats_Liao11 小时前
物联网平台MongoDB实战|(二)核心组件设计与实现
数据库·mongodb
csdn_aspnet12 小时前
使用 Docker 运行 Microsoft SQL 数据库服务器
数据库·microsoft·docker·sqlserver
FungLeo12 小时前
FengCMS · PHP+MySQL 升级实录 · 旧后台备份三宗罪:漏空表、改写内容、按分号切 SQL 直接执行
sql·mysql·php·安全加固·fengcms
SA-D12 小时前
MySQL 性能分析与库存一致性:从 Buffer Pool、B+ 树到执行计划
数据库·mysql
30955666613 小时前
股票数据落库前的去重与主键设计:一次 SQLite 写入实录
java·开发语言·jvm·数据库·redis·python·sqlite
Yeats_Liao13 小时前
物联网平台MongoDB实战|(三)性能优化与生产实践
数据库·mongodb·性能优化