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