trunc函数 & date_trunc()


PostgreSQL date_trunc() 截断日期函数,完成定时时间语法

Oracle有大量的日期函数可供使用,trunc, new_time,months_between,next_day,last_day,add_months,round等函数.当然PostgreSQl 也有大量的时间函数,详情请移步postgresql时间日期函数总结. 所以在此只说明Postgresql 中的TRUNC取断函数.

1.首先介绍一下Oracle的trunc函数:

 select trunc(sysdate) from dual    //返回当前日期  sysdate: 系统时间

select trunc(sysdate,'year') from dual;   //返回本年的第一天

select trunc(sysdate,'month') from dual;   //返回本月的第一天

select trunc(sysdate,'q') from dual;       //返回本季度的第一天

select to_char(trunc(sysdate),'yyyy-mm-dd hh24:mi:ss') from dual;  //获取当天的零时零分零秒

select trunc(sysdate,'mi') from dual;         //获取当前分

2. 接下来 介绍 Postgresql 的trunc函数.

  截断数字类型函数trunc为数字截断函数.移步postgresql: trunc函数.

  截断日期类型函数date_trunc(text,time/timestamp/timestamptz);

PostgreSQL:trunc函数

这个PostgreSQL教程解释了如何在语法和示例中使用PostgreSQL trunc函数。

描述

PostgreSQL的TRUNC函数返回一个截断到一定的小数位数。

句法

PostgreSQL中trunc函数的语法是:

trunc( number, [ decimal_places ] )

参数或参数

要截断的数字。
decimal_places
可选的。要截断的小数位数。该值必须是正整数或负整数。

注意

  • 如果decimal_places是负数,则trunc函数将使小数位0值左侧的数字成为数字。
  • 又见小区天花板地板的功能。

适用于

trunc函数可用于以下PostgreSQL版本:

  • PostgreSQL 10.1 PostgreSQL 9.6 PostgreSQL 9.4,PostgreSQL 9.3,PostgreSQL 9.2,PostgreSQL 9.1,PostgreSQL 9.0,PostgreSQL 8.4

我们来看看一些PostgreSQL trunc函数示例,并探讨如何在PostgreSQL中使用trunc函数。

例如:

postgres=# SELECT trunc(125.315);
 trunc
-------
   125
(1 row)

postgres=# SELECT trunc(125.315, 0);
 trunc
-------
   125
(1 row)

postgres=# SELECT trunc(125.315, 1);
 trunc
-------
 125.3
(1 row)

postgres=# SELECT trunc(125.315, 2);
 trunc
--------
 125.31
(1 row)

postgres=# SELECT trunc(125.315, -1);
 trunc
-------
   120
(1 row)

postgres=# SELECT trunc(125.315, -2);
 trunc
-------
   100
(1 row)

postgres=# SELECT trunc(-125.315);
 trunc
-------
  -125
(1 row)

 

3.date_trunc() 函数.

select date_trunc('month',now()) +interval '12 h';
//每月1号 12点

select date_trunc('month',now()) + interval '15 d 9 h 30 min';
//每月15号9点半

select date_trunc('day',now()) + interval '9 h';
//每天9点


select date_trunc('day',now()) + interval '7 d';
//每周的今天
select date_trunc('weak',now()) + interval '1d 1minute';
//每周的周二第一分钟

select date_trunc('h',now()) + interval '30 minute';
//每小时


select date_trunc('minute',now()) + interval '30 second';
//每分钟
select date_trunc('minute',now()) + interval '30 minute 30 second';
//每30分钟


select date_trunc('quarter',now()) + interval '15 d 15 h 15 minute 30 second';
//本季度的第15天,15小时 15分 30秒
select date_trunc('quarter',now() ) - interval '1 h';
//每个季度最后一天的晚上11点
select date_trunc('quarter',now() + interval '3 month') - interval '1 h';
//每个季度的最后一天的晚上的11点(从下个季度开始算起).

 

me=# select date_trunc('day',date '2018-05-15 09:00:00+08') + interval '9 h';
?column?
------------------------
2018-05-15 09:00:00+08
(1 行记录)

me=# select date_trunc('day',date '2018-05-15 14:09:04.127444+08') + interval '9 h';
?column?
------------------------
2018-05-15 09:00:00+08
(1 行记录)

me=# select date_trunc('day',date '2018-05-14 14:09:04.127444+08') + interval '9 h';
?column?
------------------------
2018-05-14 09:00:00+08
(1 行记录)

me=# select date_trunc('day',time '2018-05-14 14:09:04.127444+08') + interval '9 h';
?column?
----------
09:00:00
(1 行记录)

me=# select date_trunc('day',timestamp '2018-05-14 14:09:04.127444+08') + interval '9 h';
?column?
---------------------
2018-05-14 09:00:00
(1 行记录)

me=# select date_trunc('day',timestamptz '2018-05-14 14:09:04.127444+08') + interval '9 h';
?column?
------------------------
2018-05-14 09:00:00+08
(1 行记录)

me=# select date_trunc('day',timestamptz '2018-05-14 14:09:04.127444+08'+ interval '9 h');
date_trunc
------------------------
2018-05-14 00:00:00+08
(1 行记录)

 

原文:https://blog.csdn.net/liguangxianbin/article/details/80166574

 

附注:

trunc()函数用法处理日期、数字类型数据

一、日期 
TRUNC函数为指定元素而截去的日期值。 
其具体的语法格式如下: 
TRUNC(date[,fmt]) 
其中:date 一个日期值 
      fmt 日期格式,该日期将由指定的元素格式所截去。忽略它则由最近的日期截去 
如果当日日期是:2011-3-18 
1.select trunc(sysdate) from dual --2011-3-18 今天的日期为2011-3-18 
2.select trunc(sysdate, 'mm') from dual --2011-3-1 返回当月第一天. 
3.select trunc(sysdate,'yy') from dual --2011-1-1 返回当年第一天 
4.select trunc(sysdate,'dd') from dual --2011-3-18 返回当前年月日 
5.select trunc(sysdate,'yyyy') from dual --2011-1-1 返回当年第一天 
6.select trunc(sysdate,'d') from dual --2011-3-13 (星期天)返回当前星期的第一天 
7.select trunc(sysdate, 'hh') from dual --2011-3-18 14:00:00 当前时间为14:41 
8.select trunc(sysdate, 'mi') from dual --2011-3-18 14:41:00 TRUNC()函数没有秒的精确 
  
二、数字 
TRUNC(number,num_digits) 
Number 需要截尾取整的数字。 
Num_digits 用于指定取整精度的数字。Num_digits 的默认值为 0。 
TRUNC()函数截取时不进行四舍五入 
  
9.select trunc(123.458) from dual --123 
10.select trunc(123.458,0) from dual --123 
11.select trunc(123.458,1) from dual --123.4 
12.select trunc(123.458,-1) from dual --120 
13.select trunc(123.458,-4) from dual --0 
14.select trunc(123.458,4) from dual --123.458 
15.select trunc(123) from dual --123 
16.select trunc(123,1) from dual --123 
17.select trunc(123,-1) from dual --120

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM