postgresql-常用日期函数

这篇具有很好参考价值的文章主要介绍了postgresql-常用日期函数。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

简介

PostgreSQL 提供了以下日期和时间运算的算术运算符。
postgresql-常用日期函数,postgresql,postgresql,数据库
postgresql-常用日期函数,postgresql,postgresql,数据库
获取当前系统时间

select current_date,current_time,current_timestamp ;

postgresql-常用日期函数,postgresql,postgresql,数据库

-- 当前系统时间一周后的日期
select current_date + interval '7 day',current_time,current_timestamp ;

postgresql-常用日期函数,postgresql,postgresql,数据库

计算时间间隔

age(timestamp, timestamp)函数用于计算两个时间点之间的间隔,age(timestamp)函数用于
计算当前日期的凌晨 12 点到该时间点之间的间隔

select age(now(),date '1988-11-29') as ageResult;

postgresql-常用日期函数,postgresql,postgresql,数据库

获取时间中的信息

date_part(text, timestamp)和 extract(field FROM timestamp)函数用于获取日期时间中的某
一部分,例如年份、月份、小时等;date_part(text, interval)和 extract(field FROM interval)函数
用于获取时间间隔中的某一部分

-- 获取当前日期所属年份
select extract ('year' from now()) as t;

postgresql-常用日期函数,postgresql,postgresql,数据库

-- 判断当前日期是星期几
select extract ('dow' from now()) as t;

postgresql-常用日期函数,postgresql,postgresql,数据库

select date_part('year', timestamp '2020-03-03 20:38:40'), extract(year FROM
timestamp '2020-03-03 20:38:40'),
 date_part('month', interval '1 years 5 months'), extract(month FROM
interval '1 years 5 months');

postgresql-常用日期函数,postgresql,postgresql,数据库

select
	date_part('year',
	now()) as "当前年度",
	date_part('month',now()) as "当前月份",
	date_part('day',now()) as "当前日子",
	date_part('dow',now()) as "星期几"
	;

postgresql-常用日期函数,postgresql,postgresql,数据库
extract 函数实际上也是调用了 date_part 函数,只是参数方式不同。这两个函数支持获取的信息包括

  • century,世纪;
  • day,对于 timestamp,返回月份中的第几天;对于 interval,返回天数;
  • decade,年份除以 10;
  • dow,星期天(0)到星期六(6);
  • doy,一年中的第几天,(1 - 365/366);
  • epoch,对于 timestamp WITH time zone,返回 1970-01-01 00:00:00 UTC 到该时间的秒数;
    对于 date 和 timestamp,返回本地时间的 1970-01-01 00:00:00 到该时间的秒数;对于
    interval,返回以秒数表示的该时间间隔;
  • hour,小时(1 - 23);
  • isodow,ISO 8601 标准中的星期一(1)到星期天(7)
  • isoyear,ISO 8601 标准定义的日期所在的年份。每年从包含 1 月 4 日的星期一开始,2017
    年 01 月 01 日属于 2016 年;
  • microseconds,微秒,包含秒和小数秒在内的数字乘以 1000000;
  • millennium,千年;
  • milliseconds,毫秒,包含秒和小数秒在内的数字乘以 1000;
  • minute,分钟,(0 - 59);
  • month,月份;
  • quarter,季度,(1 - 4);
  • second,秒数,包含小数秒;
  • timezone,UTC 时区,单位为秒;
  • timezone_hour,UTC 时区中的小时部分;
  • timezone_minute,UTC 时区中的分钟部分;
  • week,ISO 8601 标准中的星期几,每年从第一个星期四所在的一周开始;
  • year,年份。
-- 计算当前日期从1970年到现在的秒数
select extract('epoch' from current_date);

postgresql-常用日期函数,postgresql,postgresql,数据库

截断日期/时间

date_trunc(field, source [, time_zone ])函数用于将 timestamp、timestamp WITH time zone、
date、time 或者 interval 数据截断到指定的精度
date_trunc 函数支持以下截断精度:

  • microseconds
  • milliseconds
  • second
  • minute
  • hour
  • day
  • week
  • month
  • quarter
  • year
  • decade
  • century
  • millennium
select date_trunc('year', timestamp '2020-03-03 20:38:40'),
 date_trunc('day', timestamptz '2020-03-03 20:38:40+00',
'asia/shanghai'),
 date_trunc('hour', interval '2 days 3 hours 40 minutes');

postgresql-常用日期函数,postgresql,postgresql,数据库

创建日期/时间

make_date(year int, month int, day int)函数用于创建一个日期:
make_interval(years int DEFAULT 0, months int DEFAULT 0, weeks int DEFAULT 0, days int DEFAULT 0, hours int DEFAULT 0, mins int DEFAULT 0, secs double precision DEFAULT 0.0)函数通过指定年、月、日等信息创建一个时间间隔。
make_time(hour int, min int, sec double precision)函数通过指定小时、分钟和秒数创建一个
时间。
make_timestamp(year int, month int, day int, hour int, min int, sec double precision) 函数通过指定年、月、日、时、分、秒创建一个时间戳
make_timestamptz(year int, month int, day int, hour int, min int, sec double precision, [ timezone text ])函数通过指定年、月、日、时、分、秒创建一个带时区的时间戳。如果没有指
定时区,使用当前时区
to_timestamp(double precision)函数将 Unix 时间戳(自从 1970-01-01 00:00:00+00 以来的秒
数)转换为 PostgreSQL 时间戳数据。

select make_date(2020, 03, 15) as t1,
make_interval(days => 1, hours => 5) as t2,
make_time(1, 2, 30.5) as t3,
make_timestamp(2020, 3, 15, 8, 20, 23.5) as t4,
make_timestamptz(2020, 3, 15, 8, 20, 23.5) as t5,
to_timestamp(1583152349) as t6
;

postgresql-常用日期函数,postgresql,postgresql,数据库

获取系统时间

PostgreSQL 提供了大量用于获取系统当前日期和时间的函数,例如 current_date、current_time、
current_timestamp、clock_timestamp()、localtimestamp、now()、statement_timestamp()等;同时还
支持延迟语句执行的 pg_sleep()等函数
参考文章

-- 当前日期
select current_date as t1,
current_time as t2,
localtime as t3, 
current_timestamp as t4,
localtimestamp as t5,
now() as t6
;

postgresql-常用日期函数,postgresql,postgresql,数据库

时区转换

AT TIME ZONE 运算符用于将 timestamp without time zone、timestamp WITH time zone 以及
time WITH time zone 转换为指定时区中的时间
timezone(zone, timestamp)函数等价于 SQL 标准中的 timestamp AT TIME ZONE zone。
官网介绍

select timestamp '2020-03-03 20:38:40' at time zone 'asia/shanghai',
 timestamp with time zone '2020-03-03 20:38:40-05:00' at time zone
'asia/shanghai',
 time with time zone '20:38:40-05:00' at time zone 'asia/shanghai';

postgresql-常用日期函数,postgresql,postgresql,数据库文章来源地址https://www.toymoban.com/news/detail-702637.html

到了这里,关于postgresql-常用日期函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • [pgrx开发postgresql数据库扩展]6.返回序列的函数编写(1)单值序列

    上篇文章是中规中矩的标准计算函数,就算不用pgrx,也是可以正常理解的,所以基本上没有什么对于pgrx框架有关系的东西(唯一有关系的东西,应该就是Rust的时间类型与pgrx的时间类型的计算了)。 这篇文章会讲一个pgrx对于postgresql或者说对于任何数据库扩展来说都比较有用

    2024年02月03日
    浏览(31)
  • 【数据库】PostgreSQL中使用`SELECT DISTINCT`和`SUBSTRING`函数实现去重查询

    在PostgreSQL中,我们可以使用 SELECT DISTINCT 和 SUBSTRING 函数来实现对某个字段进行去重查询。本文将介绍如何使用这两个函数来实现对 resource_version 字段的去重查询。 1. SELECT DISTINCT 语句 SELECT DISTINCT 语句用于从表中选择不重复的记录。如果没有指定列名,则会选择所有列。在本

    2024年02月14日
    浏览(30)
  • 华为GAUSS数据库常用的单行操作函数介绍

    在这篇博客里面,主要是介绍华为高斯数据库,繁多的数据类型里面,常用的函数操作的方法,然后给大家写了每个函数的用法举例,欢迎留言补充。 trim() :去除字符串左右两边的指定字符,默认是去除空格 substring(字符串, 开始序号, 连续长度)/substr() :截取指定字符串 r

    2023年04月14日
    浏览(70)
  • 图数据库_Neo4j学习cypher语言_常用函数_关系函数_字符串函数_聚合函数_数据库备份_数据库恢复---Neo4j图数据库工作笔记0008

    然后再来看一些常用函数,和字符串函数,这里举个例子,然后其他的 类似   可以看到substring字符串截取函数   可以看到截取成功   聚合函数   这里用了一个count(n) 统计函数,可以看到效果   关系函数,我们用过就是id(r) 可以取出对应的r的id来这样..

    2024年02月12日
    浏览(44)
  • 零基础学MySQL(五)-- 详细讲解数据库中的常用函数

    提供 student 表 1️⃣count 函数 count 表示返回行的总数 (1)基本语法 (2)基本练习 统计一个班级共有多少学生? 统计数学成绩大于 90 的学生有多少个? 统计总分大于 250 的人数有多少? (3)注意细节 count(*) 和 count(列) 的区别: count(*) 返回满足条件的记录的行数 count(列

    2024年01月19日
    浏览(43)
  • postgresql|数据库|MySQL数据库向postgresql数据库迁移的工具pgloader的部署和初步使用

    MySQL数据库和postgresql数据库之间的差异并不多,这里的差异指的是对SQL语言的支持两者并不大,但底层的东西差异是非常多的,例如,MySQL的innodb引擎概念,数据库用户管理,这些和postgresql相比是完全不同的(MySQL用户就是用户,没有角色,postgresql有用户,有角色,但差异不

    2024年02月14日
    浏览(69)
  • 【数据库】什么是 PostgreSQL?开源数据库系统

    PostgreSQL 是一个开源的对象关系数据库系统,本文,我们将讨论 PostgreSQL、它的用途和好处。 PostgreSQL 是由 PostgreSQL Global Development Group 开发的高级 开源关系数据库管理系统(RDBMS) 。它作为 POSTGRES 项目的一部分于 1986 年在加州大学伯克利分校启动,它最初于 1996 年 7 月 8 日发布

    2023年04月08日
    浏览(36)
  • postgresql数据库定时备份到远程数据库

    1.老规矩,服务器目录结构: conf目录无内容 profile: 其中: 最后一行 export PGPASSWORD=‘root’ 是需要备份的数据库的密码,因为直接用 pg_dump 命令备份需要输入密码交互,而我们需要达到自动备份,所以借助这种方式不需要输入密码 docker-compose.yml: 启动容器: 然后再data目录下面

    2024年02月09日
    浏览(37)
  • PostgreSQL Linux操作PostgreSQL数据库

    PostgreSQL教程 菜鸟教程:https://www.runoob.com/postgresql/postgresql-tutorial.html 登录PG数据库:psql -U 用户名(U需要大写) 登录PG数据库(指定主机、端口,并进入指定数据库): psql -U 用户名 -h 127.0.0.1 -p 5432 -d 数据库名 -U 登录的用户名 -h 连接的主机(默认127.0.0.1,可替换成远程主机

    2024年02月11日
    浏览(48)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包