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') 日期

2012-05-17 09:32 466人阅读 评论(0) 收藏 举报
  1. select trunc(sysdate ,'dd') from dual ;   --  2007-9-19
  2. select trunc(sysdate ,'yyyy') from dual ;   --2007-1-1
  3. select trunc(sysdate ,'mm') from dual ;   --2007-9-1
  4. begin
  5. dbms_output.put_line( to_char ( (sysdate)    , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;
  6. dbms_output.put_line( to_char ( (sysdate)+ 1/24/60/10   , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;
  7. dbms_output.put_line( to_char (  ((sysdate)+ 10 / ( 24*60*60 )   )  , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;
  8. dbms_output.put_line( to_char (  trunc((sysdate)+ 10 / ( 24*60*60 )   )  , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;
  9. end ;
  10. /
  11. begin
  12. dbms_output.put_line( '当前时间 '  ) ;
  13. dbms_output.put_line( to_char ( (sysdate)    , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;
  14. dbms_output.put_line( '当前时间  + 1  s  '    ) ;
  15. dbms_output.put_line( to_char ( (sysdate)+ (((1/24)/60)/60   )   , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;
  16. dbms_output.put_line( '当前时间  + 1  s  '    ) ;
  17. dbms_output.put_line( to_char ( (sysdate)+ (((5/24)/60)/60   )   , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;
  18. dbms_output.put_line( '当前时间  + 10s  '   ) ;
  19. dbms_output.put_line( to_char (  ((sysdate)+  ( 10 / ( 24*60*60 ))    )  , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;
  20. dbms_output.put_line( '当前 日   '   ) ;
  21. dbms_output.put_line( to_char (  trunc((sysdate))  , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;
  22. dbms_output.put_line( '当前  第2天 1点  '   ) ;
  23. dbms_output.put_line( to_char (  trunc(sysdate)+(  1 +  1/24   ) , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;
  24. dbms_output.put_line( '当前  第2天 9点  '   ) ;
  25. dbms_output.put_line( to_char (  trunc(sysdate)+(  1 +  9/24   ) , 'yyyy-mm-dd hh24:mi:ss'  ) ) ;
  26. end ;
  27. /

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

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

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

最新文章

  1. wordpress语言切换
  2. 跟我一起玩转Sencha Touch 移动 WebApp 开发(一)
  3. S2 易买网总结
  4. day11
  5. Javascript -- toFixed()函数
  6. 使用Jquery promise 动态引入js文件
  7. zookeeper_04:curator
  8. Bootstrap-dialog的使用(续Bootstrap Table)
  9. dev 中的GridControl中的行实现选择的功能实现
  10. Kafka 客户端实现逻辑分析
  11. WeQuant交易策略—Chaikin A/D
  12. 声明式RESTful客户端在asp.net core中的应用
  13. 解决Ubuntu无法通过ssh远程登录问题
  14. Sql Server数据库之事务,视图,索引
  15. Lua语言特色
  16. MySQL视图(view)
  17. zabbix已恢复正常,但是报警信息一直出现,求大佬解答。
  18. Mask R-CNN论文理解
  19. 【Android UI】使用RelativeLayout与TableLayout实现登录界面
  20. data lake 新式数据仓库

热门文章

  1. caffe 网络参数设置
  2. 7.解决谷歌的SDK更新失败问题。
  3. 语法之ADO.NET
  4. Rnqoj (未出现的子串)
  5. org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x160fd6f04410017 after 0ms
  6. zookeeper简单操作
  7. 拨打电话<a href="tel:">跳转到邮件<a href="mailto:">
  8. SetForegroundWindow以及 如何将一个某个窗口提到最顶层(转)
  9. delphi ----日期控件运用,日期问题,日期时间比较
  10. yum -y install epel-release