首页 > 数据库 >postgresql-时间函数

postgresql-时间函数

时间:2024-02-05 10:47:42浏览次数:35  
标签:postgresql 函数 -- timestamp 09 时间 2022 date select

PostgreSQL-version 9.4.24

1. 系统当前时间

select now();   -- 天、时分秒 2022-09-29 15:50:13.273269+08

select current_date;    -- 天 2022-09-29

select current_time;    -- 时分秒 15:50:13.507701+08

select current_timestamp;  -- 天、时分秒 2022-09-29 15:50:13.522212+08

select localtimestamp;   -- 不带时区,2022-09-29 20:47:25.046437

select localtime;

select clock_timestamp();  -- 带时区

2. 格式转换

-- select to_timestamp(double precision)
select to_timestamp(1663740005);  -- unix时间戳转时间

select date(to_timestamp(1663743934638::numeric / 1000));  -- 时间保留到天

select date(localtimestamp);

-- select to_date(text, text)
select to_date('05 Dec 2022', 'DD Mon YYYY');   -- 字符串转日期

-- select to_char(timestamp, text)
select to_char(now(), 'YYYY-MM-DD HH:MI:SS');   -- 时间转字符串


select timestamp '2022-09-29 10:23:18';

select date '2022-09-29 10:23:18';

select time '2022-09-29 10:23:18';

select timestamp with time zone '2022-09-29 10:23:18';

select timestamp 'epoch' + 1663740005 * interval '1 second';

3. 时间计算

select now() + interval '2 day';
select now() + interval '2 days';
select now() + interval '2 hour';
select now() + interval '2 minute';
select now() + interval '2 second';

-- select age(timestamp, timestamp)
select age('2022-09-21', '2022-09-29'); -- -8days

select age(date '2022-09-21');  -- 当前时间与输入时间的间隔

interval部分也可以不写,其后的值有以下形式

Abbreviation Meaning
Y Years
M Months (in the date part)
W Weeks
D Days
H Hours
M Minutes (in the time part)
S Seconds

4. 时间截取

-- select date_part(text, timestamp)
select date_part('year' , now());

-- select extract(field from timestamp)
select extract(year from now());

-- select DATE_TRUNC(text, timestamp)
select date_trunc('hour', now());
select date_trunc('day', timestamp '2022-09-28 14:25:01');

参考:
http://postgres.cn/docs/9.4/functions-datetime.html

标签:postgresql,函数,--,timestamp,09,时间,2022,date,select
From: https://www.cnblogs.com/wxyz94/p/16741868.html

相关文章

  • mysql-自定义函数
    0、结构delimiter$$--修改语句结束符,mysql默认的结束符是分号,为了在函数体中避免遇到分号就结束,重新定义结束符createfunction函数名(形参)returns返回值类型begin//函数体return返回值数据;end$$delimiter;--将语句结束符修改回来1、变......
  • 1.24 stat、lstat函数
     摘自:https://blog.csdn.net/m0_38062470/article/details/113574697 #include<sys/types.h>#include<sys/stat.h>#include<unistd.h>intstat(constchar*pathname,structstat*buf);作用:获取文件相关的信息参数:-pathname:操作的文件的路径-buf:结构......
  • 【CentOS系统解决系统时间与本地时间不同步】
    一、方案一查看系统时间与硬件时间#查看系统时间date#查看硬件时间hwclock删除本地时间rm-rf/etc/localtime创建软连接ln-s/usr/share/zoneinfo/Asia/Shanghai/etc/localtime时间同步yum-yinstallntpntpdatentpdatentp1.aliyun.com校准硬件时......
  • 前端监听页面是否鼠标移动 超过一定的静止时间自动跳转到登录页面
    import{CreateBuriedPoints}from'@/api/Statistics';exportconstDurationStay={data(){return{currentTime:"",DurationOfStay:5*60*1000,//自定义的无操作时长5分钟intervalTime:0}},mou......
  • 【数据库】PostgreSQL中的DISTINCT ON和DISTINCT的区别
    深入理解PostgreSQL中的DISTINCTON和DISTINCT在数据库查询中,我们经常会遇到需要去除重复数据的情况。在PostgreSQL中,我们可以使用DISTINCT和DISTINCTON来实现这个目标。那么,它们之间有什么区别呢?本文将详细介绍这两种方法的用法、区别以及适用场景。DISTINCT的基本用法DISTIN......
  • 内联函数的出现
    inline_func在c++中,预定义宏的概念是用内联函数来实现的,而内联函数本身也是一个真正的函数。内联函数具有普通函数的所有行为。唯一不同之处在于它在编译时会被直接插入到调用它的地方,而不是像普通函数那样在运行时被调用。这样,内联函数可以避免函数调用的开销,提高代码执行效率,所......
  • C++CLI 析构函数和终结器理解
    测试类:#pragmaoncerefclassHello{public:Hello();~Hello();!Hello();};/***************/#include"Hello.h"Hello::Hello(){System::Console::WriteLine("构造函数!");}Hello::~Hello(){System::Console::WriteLi......
  • 无涯教程-setMonth()函数
    JavaScriptdatesetMonth()方法设置Date对象中月份(0~11)。setMonth()-语法Date.setMonth(monthValue[,dayValue])monthValue  - 0到11之间的整数(代表从一月到十二月的月份)。dayValue    - 从1到31的整数,代表一个月中的某天。msValu......
  • [office] 学会LOOKUP函数这个高级用法,多条件查询就很容易了!
    如下表,是某快递公司价格表,每当查询价格时,会涉及很多条件,始发地、目的地、重量区域等,在全部条件判断完之后,还得与最低价进行比较,取两者之间的最大值。价格表如下:查询表如下:举例,始发地为义乌,目的地是北京,重量为1680.57,对应价格为1.6。金额为:=1680.57*1.6,算出金额之后,再与最低价200相......
  • 无涯教程-setMinutes()函数
    JavaScriptdatesetMinutes()方法设置Date对象中的分钟(0~59)。setMinutes()-语法Date.setMinutes(minutesValue[,secondsValue[,msValue]])minutesValue  - 0到59之间的整数,代表分钟。secondsValue  - 0到59之间的整数,代表秒。如果指定secon......