oracle 时间函数 TRUNC (date)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/uotail/article/details/83176241

TRUNC(date [, fmt ])

 https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions201.htm

Purpose

The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. The value returned is always of datatype DATE, even if you specify a different datetime datatype for date. If you omit fmt, then date is truncated to the nearest day. Please refer to "ROUND and TRUNC Date Functions" for the permitted format models to use in fmt.

 TRUNC(date)函数返回截断到指定单位的date。返回的值始终为数据类型DATE,即使您为其指定了不同的datetime数据类型date。如果省略fmt,则date截断到最近的一天。有关允许使用的格式模型,请参阅“ROUND和TRUNC日期函数”fmt

https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions230.htm#i1002084 

The default model, 'DD', returns the date rounded or truncated to the day with a time of midnight. 

默认模型“DD”将日期舍入或截断为具有午夜时间的日期

 如

select trunc(sysdate) from dual

select to_char(trunc(sysdate),'YYYY-MM-DD HH:MI:SS') from dual 

 

Format Model Rounding or Truncating Unit
CC
SCC

One greater than the first two digits of a four-digit year

意思是  一个比四位数的年份的前两位大一

比如 2018 前两位加1 则为 2001 

 select trunc(sysdate,'CC') from dual

用字符串完整显示上面的时间 

select to_char(trunc(sysdate,'CC'),'YYYY-MM-DD HH24:MI:SS') from dual

 

即CC 格式将日期截取后 年取前两位加一,后面为1月1日的午夜时间 

select trunc(TO_DATE('2218-10-18','YYYY-MM-DD'),'CC') from dual

 

SYYYY
YYYY
YEAR
SYEAR
YYY
YY
Y

Year (rounds up on July 1)

 select to_char(trunc(sysdate,'Y'),'YYYY-MM-DD HH24:MI:SS') from dual

直接将年后的数据置为1月1日的午夜时间

 

IYYY
IY
IY
I

ISO Year

 select to_char(trunc(sysdate,'IY'),'YYYY-MM-DD HH24:MI:SS') from dual

 应该是 符合 ISO标准的时间

Q

Quarter (rounds up on the sixteenth day of the second month of the quarter)

 Quarter 四份之一 季度的意思

 select to_char(trunc(sysdate,'Q'),'YYYY-MM-DD HH24:MI:SS') from dual
select to_char(trunc(TO_DATE('2018-10-22','YYYY-MM-DD'),'Q'),'YYYY-MM-DD HH24:MI:SS') from dual

  

对于trunc来说这个好像是截取到月 ,月后为当月1日的午夜时间 

其实不是,在看一个例子

select to_char(trunc(TO_DATE('2018-01-22','YYYY-MM-DD'),'Q'),'YYYY-MM-DD HH24:MI:SS__Q') from dualselect to_char(trunc(TO_DATE('2018-03-22','YYYY-MM-DD'),'Q'),'YYYY-MM-DD HH24:MI:SS__Q') from dual

 第一季度

select to_char(trunc(sysdate,'MON'),'YYYY-MM-DD HH24:MI:SS__Q') from dual

 第四季度

所以trunc的真正作用是截取到当月所属季度的开始月份的一月一日的午夜时间

MONTH
MON
MM
RM

Month (rounds up on the sixteenth day)

select to_char(trunc(sysdate,'MON'),'YYYY-MM-DD HH24:MI:SS') from dual
select to_char(trunc(TO_DATE('2018-10-22','YYYY-MM-DD'),'MON'),'YYYY-MM-DD HH24:MI:SS') from dual 

 截取到当月1号的午夜时间

WW

Same day of the week as the first day of the year

这个没理解 

 select to_char(trunc(sysdate,'WW'),'YYYY-MM-DD HH24:MI:SS__WW') from dual

IW

Same day of the week as the first day of the ISO year

W

Same day of the week as the first day of the month

DDD
DD
J

Day

 这个就是开头测试的 截取到当日的午夜时间,也是fmt的默认格式

DAY
DY
D

Starting day of the week

select to_char(trunc(sysdate,'DAY'),'YYYY-MM-DD HH24:MI:SS__DAY') from dual

 

截取到这周的开始 即星期日的午夜时间

HH
HH12
HH24

Hour

 select to_char(trunc(sysdate,'HH24'),'YYYY-MM-DD HH24:MI:SS') from dual

J截取到当前小时的零分灵妙

MI

Minute

截取到分  ,可见分是能截取到的最小单位 

猜你喜欢

转载自blog.csdn.net/uotail/article/details/83176241