postgresql 日期查询最全整理

 更新时间:2024年08月07日 10:45:24   作者:gis分享者  
这篇文章主要介绍了postgresql 日期查询最全整理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

1、获取当前日期

select now();

在这里插入图片描述

select current_timestamp;

在这里插入图片描述

返回值均是当前年月日、时分秒,且秒保留6位小数,两种方式等价

select current_time;

在这里插入图片描述

返回值:时分秒,秒最高精确到6位

select current_date;

在这里插入图片描述

返回值:年月日

2、查询今天的数据

SELECT * FROM 表名 WHERE 时间字段 >= current_date AND 时间字段 < current_date + 1;

3、查询昨天的数据

SELECT * FROM 表名 WHERE 时间字段 >= current_date - 1 AND 时间字段 < current_date;

4、查询一个月内的数据

SELECT * FROM 表名 WHERE 时间字段 >= current_date - interval '1 month' AND 时间字段 <= current_date;

5、按日, 周, 月, 季度, 年统计数据

select date_trunc('DAY', 时间字段) as statisticTime, 分组字段, count(0) from 表名 GROUP BY date_trunc('DAY', 时间字段), 分组字段

日: DAY; 周: WEEK; 月: MONTH; 季度: QUARTER; 年: YEAR

6、 查询昨天、上周、上月、上年的日期

select to_char( now() - interval '1 day','yyyy-mm-dd');
select to_char( now() - interval '1 week','yyyy-mm-dd hh:mi:ss');
select to_char( now() - interval '1 month','yyyy-mm-dd');
select to_char( now() - interval '1 year','yyyy-mm-dd');

7、查询今天、今月、今年的开始的日期时间

select date_trunc('year', now())
select date_trunc('month', now())
select date_trunc('day', now())
select date_trunc('hour', now())
select date_trunc('minute', now())
select date_trunc('second', now())

8、查询最近1秒,1分,1小时,1天,1周(7天),1月,1年的记录

select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 seconds '
select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 minutes'
select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 hours'
select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 day'
select * from 表名 where timestamp_start >= current_timestamp - interval ' 7 day'
select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 month'
select * from 表名 where timestamp_start >= current_timestamp - interval ' 1 year'

9、从时间戳中提取 年月日时分秒、周

select date_part('year', timestamp '2024-02-16 12:38:40')
select date_part('month', timestamp '2024-02-16 12:38:40')
select date_part('day', timestamp '2024-02-16 12:38:40')
select date_part('hour', timestamp '2024-02-16 12:38:40')
select date_part('minute', timestamp '2024-02-16 12:38:40')
select date_part('second', timestamp '2024-02-16 12:38:40')
select date_part('week', timestamp '2024-02-16 12:38:40')

到此这篇关于postgresql 您要的日期查询都在这的文章就介绍到这了,更多相关postgresql 日期查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • PostgreSQL中数据批量导入导出的错误处理

    PostgreSQL中数据批量导入导出的错误处理

    在 PostgreSQL 中进行数据的批量导入导出是常见的操作,但有时可能会遇到各种错误,下面将详细探讨可能出现的错误类型、原因及相应的解决方案,并提供具体的示例来帮助您更好地理解和处理这些问题,需要的朋友可以参考下
    2024-07-07
  • postgres 使用存储过程批量插入数据的操作

    postgres 使用存储过程批量插入数据的操作

    这篇文章主要介绍了postgres 使用存储过程批量插入数据的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • postgresql初始化之initdb的使用详解

    postgresql初始化之initdb的使用详解

    这篇文章主要介绍了postgresql初始化之initdb的使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • PostgreSQL 删除check约束的实现

    PostgreSQL 删除check约束的实现

    这篇文章主要介绍了PostgreSQL 删除check约束的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • postgresql json取值慢的原因分析

    postgresql json取值慢的原因分析

    这篇文章主要介绍了postgresql json取值为何这么慢,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06
  • postgresql 中的加密扩展插件pgcrypto用法说明

    postgresql 中的加密扩展插件pgcrypto用法说明

    这篇文章主要介绍了postgresql 中的加密扩展插件pgcrypto用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • postgresql IvorySQL新增命令及相关配置参数详解

    postgresql IvorySQL新增命令及相关配置参数详解

    这篇文章主要为大家介绍了postgresql IvorySQL新增命令及相关配置参数详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • PostgreSQL15.x安装的详细教程

    PostgreSQL15.x安装的详细教程

    PostgreSQL 是一个功能强大的开源关系型数据库系统,基于 C 语言实现,采用 PostgreSQL 许可证,这是一种自由软件许可证,允许用户自由使用、修改和分发源代码,所以本文将给大家介绍PostgreSQL15.x安装的详细教程,需要的朋友可以参考下
    2024-09-09
  • Ruoyi从mysql切换到postgresql的几个踩坑实战

    Ruoyi从mysql切换到postgresql的几个踩坑实战

    最近由于工作的原因,需要将Ruoyi从mysql切换到postgresql,所以这篇文章主要给大家介绍了关于Ruoyi从mysql切换到postgresql的几个踩坑实战,需要的朋友可以参考下
    2023-02-02
  • 本地计算机上的 postgresql 服务启动后停止的问题解决

    本地计算机上的 postgresql 服务启动后停止的问题解决

    这篇文章主要介绍了本地计算机上的 postgresql 服务启动后停止的问题解决,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01

最新评论