TRUNC函数的用法

TRUNC函数用于对值进行截断。

用法有两种:TRUNC(NUMBER)表示截断数字,TRUNC(date)表示截断日期。

(1)截断数字:

格式:TRUNC(n1,n2),n1表示被截断的数字,n2表示要截断到那一位。n2可以是负数,表示截断小数点前。注意,TRUNC截断不是四舍五入。

SQL> select TRUNC(15.79) from dual;

TRUNC(15.79)
------------
          15

SQL> select TRUNC(15.79,1) from dual;

TRUNC(15.79,1)
--------------
          15.7

SQL> select trunc(15.79,-1) from dual;

TRUNC(15.79,-1)
---------------
             10

(2)截断日期:

先执行命令:alter session set nls_date_format='yyyy-mm-dd hh24:mi:hh';

截取今天:

SQL> select sysdate,trunc(sysdate,'dd') from dual;

SYSDATE             TRUNC(SYSDATE,'DD')
------------------- -------------------
2009-03-24 21:31:17 2009-03-24 00:00:00

截取本周第一天:

SQL> select sysdate,trunc(sysdate,'d') from dual;

SYSDATE             TRUNC(SYSDATE,'D')
------------------- -------------------
2009-03-24 21:29:32 2009-03-22 00:00:00

截取本月第一天:

SQL> select sysdate,trunc(sysdate,'mm') from dual;

SYSDATE             TRUNC(SYSDATE,'MM')
------------------- -------------------
2009-03-24 21:30:30 2009-03-01 00:00:00

截取本年第一天:

SQL> select sysdate,trunc(sysdate,'y') from dual;

SYSDATE             TRUNC(SYSDATE,'Y')
------------------- -------------------
2009-03-24 21:31:57 2009-01-01 00:00:00

截取到小时:

SQL> select sysdate,trunc(sysdate,'hh') from dual;

SYSDATE             TRUNC(SYSDATE,'HH')
------------------- -------------------
2009-03-24 21:32:59 2009-03-24 21:00:00

截取到分钟:

SQL> select sysdate,trunc(sysdate,'mi') from dual;

SYSDATE             TRUNC(SYSDATE,'MI')
------------------- -------------------
2009-03-24 21:33:32 2009-03-24 21:33:00


获取上月第一天:

SQL> select TRUNC(add_months(SYSDATE,-1),'MM') from dual

===================================================================

--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

========================================================================================================

oracle trunc(sysdate ,'dd') 日期

[sql] view plain copy
 
  1. select trunc(sysdate ,'dd') from dual ;   --  2007-9-19  
  2.   
  3. select trunc(sysdate ,'yyyy') from dual ;   --2007-1-1  
  4.   
  5. select trunc(sysdate ,'mm') from dual ;   --2007-9-1  
  6.   
  7.   
  8.   
  9. begin   
  10. dbms_output.put_line( to_char ( (sysdate)    , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;  
  11. dbms_output.put_line( to_char ( (sysdate)+ 1/24/60/10   , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;  
  12. dbms_output.put_line( to_char (  ((sysdate)+ 10 / ( 24*60*60 )   )  , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;  
  13.   
  14. dbms_output.put_line( to_char (  trunc((sysdate)+ 10 / ( 24*60*60 )   )  , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;  
  15.   
  16. end ;   
  17. /  
  18.   
  19.    
  20.   
  21.    
  22.   
  23.   
  24. begin   
  25. dbms_output.put_line( '当前时间 '  ) ;  
  26. dbms_output.put_line( to_char ( (sysdate)    , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;  
  27.   
  28. dbms_output.put_line( '当前时间  + 1  s  '    ) ;  
  29. dbms_output.put_line( to_char ( (sysdate)+ (((1/24)/60)/60   )   , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;  
  30.   
  31. dbms_output.put_line( '当前时间  + 1  s  '    ) ;  
  32. dbms_output.put_line( to_char ( (sysdate)+ (((5/24)/60)/60   )   , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;  
  33.   
  34. dbms_output.put_line( '当前时间  + 10s  '   ) ;  
  35. dbms_output.put_line( to_char (  ((sysdate)+  ( 10 / ( 24*60*60 ))    )  , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;  
  36.   
  37. dbms_output.put_line( '当前 日   '   ) ;  
  38. dbms_output.put_line( to_char (  trunc((sysdate))  , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;  
  39.   
  40.   
  41. dbms_output.put_line( '当前  第2天 1点  '   ) ;  
  42. dbms_output.put_line( to_char (  trunc(sysdate)+(  1 +  1/24   ) , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;  
  43.   
  44.   
  45. dbms_output.put_line( '当前  第2天 9点  '   ) ;  
  46. dbms_output.put_line( to_char (  trunc(sysdate)+(  1 +  9/24   ) , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;  
  47.   
  48.    
  49.   
  50.   
  51. end ;   
  52. /  

 

引用原文:http://blog.csdn.net/haiross/article/details/12837033

写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权。希望尽自己的努力,做到更好,大家一起努力进步!

如果有什么问题,欢迎大家一起探讨,代码如有问题,欢迎各位大神指正!

原文地址:https://www.cnblogs.com/summary-2017/p/7761177.html