iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >PostgreSql日期类型处理详细实例
  • 666
分享到

PostgreSql日期类型处理详细实例

postgresql日期类型处理postgresql日期 2023-05-16 11:05:59 666人浏览 薄情痞子
摘要

目录1. 查询天数据2. 查询月数据3. 查询年数据4.类型转换补充:时区转换总结1. 查询天数据 查询当天数据 select * from table1 as n where n.created_t

1. 查询天数据

查询当天数据

select * from table1 as n
where n.created_time>=current_date;

查询昨天数据

select * from table1 as n
where n.created_time>=current_date-1 and n.created_time <current_date ;

2. 查询月数据

查询当月数据

select *
from table1 as n
WHERE extract(YEAR FROM created_time) = extract(YEAR FROM now())
and extract(MONTH FROM created_time) = extract(MONTH FROM now()) 

查询上月数据

select *
from table1 as n
where created_time >= date_trunc('month',current_date - interval '1' month)
and created_time < date_trunc('month',current_date)

3. 查询年数据

查询当年数据

select *
from table1 as n
WHERE extract(YEAR FROM created_time) = extract(YEAR FROM now()) ORDER BY created_time

查询去年数据

select *
from table1 as n
where created_time >= date_trunc('year',current_date - interval '1' year)
and created_time < date_trunc('year',current_date)

4.类型转换

1.查询某天:datetime类型的,需要转换为 date 类型,如果你要查询的字段已经是 date 类型则不需要进行转换

select t_create
from table
where t_create::date = to_date(‘2023-02-08', ‘YYYY-MM-DD');

2.string转timestamp类型,按范围查询

select * from table where create_date >= ‘2023-01-08'::timestamp and create_date < ‘2023-02-08'::timestamp;

3.时间戳Long转Timestamp

select TO_TIMESTAMP(1512490630)

4.string转data,只能得到年月日,得不到时分秒

select to_date(‘2023-01-28 12:55:05')

5.当前日期 select current_date

6.带时区的时分秒值 select current_time;也可以使用current_time(precision),将结果在四分之一秒的范围内四舍五入到位数,比如select current_time(2);对应没有时区的值:select localtime;

7.带时区的年月日时分秒值 select current_timestamp; 对应没有时区的值:select localtimestamp;

补充:时区转换

有些时候,时区转换对于特定时间在不同时区显示特别有用。AT TIME ZONE提供了这种功能,它是如何做到的?我们将在一个事务中进行演示,因为同一事务中now()函数总是返回相同的值,从而我们可以很容易看到同一时间在不同时区显示的差别。

postgres=# BEGIN;
BEGIN
postgres=# SELECT now();
       now
-------------------------------
 2013-08-26 12:39:39.122218+02
postgres=# SELECT now() AT TIME ZONE 'GMT';
     timezone
----------------------------
 2013-08-26 10:39:39.122218
postgres=# SELECT now() AT TIME ZONE 'GMT+1';
     timezone
----------------------------
 2013-08-26 09:39:39.122218
postgres=# SELECT now() AT TIME ZONE 'PST';
     timezone
----------------------------
 2013-08-26 02:39:39.122218

总结

到此这篇关于postgresql日期类型处理的文章就介绍到这了,更多相关Postgresql日期类型处理内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

您可能感兴趣的文档:

--结束END--

本文标题: PostgreSql日期类型处理详细实例

本文链接: https://www.lsjlt.com/news/209822.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作