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

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

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

TRUNC函数的用法的更多相关文章

  1. Oracle trunc()函数的用法

    Oracle trunc()函数的用法 /**************日期********************/1.select trunc(sysdate) from dual --2013-0 ...

  2. oracle获取本月第一天和最后一天及Oracle trunc()函数的用法

    select to_char(trunc(add_months(last_day(sysdate), -1) + 1), 'yyyy-mm-dd') "本月第一天", to_cha ...

  3. Oracle trunc()函数的用法--来着心静禅定ing

    1.TRUNC(for dates) TRUNC函数为指定元素而截去的日期值. 其具体的语法格式如下: TRUNC(date[,fmt]) 其中: date 一个日期值 fmt 日期格式,该日期将由指 ...

  4. Oracle trunc()函数的用法及四舍五入 round函数

    --Oracle trunc()函数的用法/**************日期********************/1.select trunc(sysdate) from dual  --2011 ...

  5. [转]Oracle trunc()函数的用法

    原文地址:http://www.cnblogs.com/gengaixue/archive/2012/11/21/2781037.html 1.TRUNC(for dates) TRUNC函数为指定元 ...

  6. 有关日期的函数操作用法总结,to_date(),trunc(),add_months();

    相关知识链接: Oracle trunc()函数的用法 oracle add_months函数 Oracle日期格式转换,tochar(),todate() №2:取得当前日期是一个星期中的第几天,注 ...

  7. Oracle trunc()函数,decode()函数,substr函数,GREATEST函数,java中substring函数的用法

    --Oracle trunc()函数的用法/**************日期********************/1.select trunc(sysdate) from dual --2013- ...

  8. oracle的round函数和trunc函数

    --Oracle trunc()函数的用法/**************日期********************/1.select trunc(sysdate) from dual --2013- ...

  9. trunc函数

    date 为必要参数,是输入的一个日期值 fmt 参数可忽略,是日期格式,用以指定的元素格式来截去输入的日期值.忽略它则由最近的日期截去 下面是该函数的使用情况: trunc(sysdate,'yyy ...

随机推荐

  1. VC++ 带界面的ActiveX控件

    一.新建MFC ActiveX工程OleHasInterface: 二.新建一个对话框资源,ID为 IDD_FORMVIEW,关联类CActXInterfaceDlg,基类CDialog: 三.设计对 ...

  2. VC++显示文件或文件夹属性

    When you select a file or folder in Explorer window, and choose 'Properties' from the menu, you get ...

  3. .net webapi项目跨域问题及解决方案

    问题: 1.项目完成,部署到不同的iis版本上,跨域访问有的通有的不通 解决办法: 1.将复杂请求改为简单请求 2.代码中去掉所有跨域设置,配置中添加或修改节点 <system.webServe ...

  4. GIT 回退出错 Unlink of file 'xx' failed. Should I try again? (y/n) 解决办法

    发生过程 回退版本 如果回退版本时 里面有删除或者移动的文件 容易出这个问题 解决方法 git reset --hard 版本号  回退失败了  就 本地工作目录跟版本那个工作目录比较   然后还原修 ...

  5. 【Cygwin】Windows下使用linux命令

    我参阅了这份文章: 让windows cmd也用上linux命令 原文时间有点久了,Cygwin也更新了... 所以我的做法简单了很多... 到Cygwin官网下载安装包:https://cygwin ...

  6. hiho一下第107周《Give My Text Back》

    题目链接:传送门 题目大意:给你多组格式不标准的字符串句子,要你恢复到标准格式(单词中间只有一个空格或者一个逗号一个空格,句子的首字母大写其他小写,遇到回车也要换行) 题目思路:直接按题意模拟,注意几 ...

  7. Mac中pico编辑器的使用方法

    Pico是一个由华盛顿大学(University of Washington)计算与通讯研究所(Computing and Communications Group)编写并维护的文本编辑程序,在多个版 ...

  8. 巨蟒python全栈开发-第9天 初识函数

    一.今日主要内容总览(重点) 1.什么是函数? f(x)=x+1 y=x+1 函数是对功能或者动作的封装2.函数的语法和定义 def 函数名(): 函数体 调用:函数名()3.关于函数的返回值 ret ...

  9. 网站漏洞扫描工具(appscan,mdcsoft-ips)

    网站漏洞扫描工具:主要应用网站漏洞扫描工具,其原理是通过工具通过对网站的代码阅读,发现其可被利用的漏洞进行告示,通过前人收集的漏洞编成数据库,根据其扫描对比做出. 具体网站扫描工具有:appscan, ...

  10. 找不到ifconfig命令

    对于新安装的系统,可能会缺少ifconfig命令,这是因为少安装了net-tools工具,所以只要安装上即可. yum install net-tools -y