Heim  >  Artikel  >  Datenbank  >  Oracle trunc()函数的用法

Oracle trunc()函数的用法

WBOY
WBOYOriginal
2016-06-07 17:46:252036Durchsuche

本文章来介绍Oracle trunc()函数的用法TRUNC函数的format,自己现在有点体会,format为day时,只精确到天,而不管几年几月只要是符合的day就可以了,要想确定一年中的某月的某一天就要用trunc(date,\'\'dd\'\').

本文章来介绍Oracle trunc()函数的用法TRUNC函数的format,自己现在有点体会,format为day时,只精确到天,而不管几年几月只要是符合的day就可以了,要想确定一年中的某月的某一天就要用trunc(date,\'\'dd\'\').

format为年时,精确到-----年

为月时,精确到------年,月(不管哪年,只要是相同的月和哪天)
为日时,精确到------年,月,日(不管哪年的哪月,只关心是哪天)

1.TRUNC(for dates)
TRUNC函数为指定元素而截去的日期值。
其具体的语法格式如下:
TRUNC(date[,fmt])
其中:
date 一个日期值
fmt 日期格式,该日期将由指定的元素格式所截去。忽略它则由最近的日期截去
下面是该函数的使用情况:

 代码如下 复制代码
TRUNC(TO_DATE(’24-Nov-1999 08:00 pm’,’dd-mon-yyyy hh:mi am’))
=’24-Nov-1999 12:00:00 am’
TRUNC(TO_DATE(’24-Nov-1999 08:37 pm’,’dd-mon-yyyy hh:mi am’,’hh’)) =’24-Nov-1999 08:00:00 am’

round (date,''format'')未指定format时,如果日期中的时间在中午之前,则将日期中的时间截断为12 A.M.(午夜,一天的开始),否则进到第二天。

TRUNC(date,''format'')未指定format时,将日期截为12 A.M.,不考虑是否在中午之前的条件。

2.TRUNC(for number)
TRUNC函数返回处理后的数值,其工作机制与ROUND函数极为类似,只是该函数不对指定小数前或后的部分做相应舍入选择处理,而统统截去。
其具体的语法格式如下
TRUNC(number[,decimals])
其中:
number 待做截取处理的数值
decimals 指明需保留小数点后面的位数。可选项,忽略它则截去所有的小数部分
下面是该函数的使用情况:
TRUNC(89.985,2)=89.98
TRUNC(89.985)=89
TRUNC(89.985,-1)=80
注意:第二个参数可以为负数,表示为小数点左边指定位数后面的部分截去,即均以0记。
 
--Oracle trunc()函数的用法
/**************日期********************/

 代码如下 复制代码

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

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;  --显示当前时间
2011-12-29 16:24:34

select trunc(sysdate,'year') from dual; --截取到年(本年的第一天)
2011-1-1

select trunc(sysdate,'q') from dual; --截取到季度(本季度的第一天)
2011-10-1

select trunc(sysdate,'month') from dual; --截取到月(本月的第一天)
2011-12-1

select trunc(sysdate,'') from dual;

select to_char(trunc(sysdate),'yyyymmdd hh24:mi:ss') from dual; --默认截取到日(当日的零点零分零秒)
20111229 00:00:00

select trunc(sysdate-1,'w') from dual;  -- 离当前时间最近的周四,若当天为周四则返回当天,否则返回上周四
2011-12-22

select trunc(sysdate,'ww') from dual;  --截取到上周末(上周周六)
2011-12-24

select trunc(sysdate,'day') from dual; --截取到周(本周第一天,即上周日)
2011-12-25

select trunc(sysdate,'iw') from dual; --本周第2天,即本周一
2011-12-26

select to_char(trunc(sysdate,'dd'),'yyyymmdd hh24:mi:ss') from dual;--截取到日(当日的零点零分零秒)
20111229 00:00:00

select trunc(sysdate,'hh24') from dual;  --截取到小时(当前小时,零分零秒)
2011-12-29 16:00:00

select trunc(sysdate,'mi') from dual; --截取到分(当前分,零秒)
2011-12-29 16:24:00

select trunc(sysdate,'ss') from dual ;--报错,没有精确到秒的格式

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn