zoukankan      html  css  js  c++  java
  • Oracle trunc函数

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

    TRUNC(for dates)

    TRUNC函数为指定元素而截去的日期值。

    其具体的语法格式如下:

        Oracle TRUNC(date[,fmt]) 
    其中:

    date 日期值

    fmt 日期格式,按该格式截取日期。不指定,默认为年月日形式。

    看测试:

    --更改Oracle默认的系统时间形式显示为yyyy-mm-dd hh24:mi:ss格式

    SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'yyyy-mm-dd hh24:mi:ss';

    会话已更改。

    SQL> SELECT TRUNC(sysdate) FROM dual; (显示为当前时间的年月日,时分秒为00)

    TRUNC(SYSDATE)                                                                 

    -------------------                                                             

    2012-07-20 00:00:00                                                            

    SQL> SELECT TRUNC(sysdate,'yyyy') FROM dual;(显示当前年的第一天)

    TRUNC(SYSDATE,'YYYY                                                            

    -------------------                                                            

    2012-01-01 00:00:00                                                            

    SQL> SELECT TRUNC(sysdate,'mm') FROM dual;(显示当前年月的第一天)

    TRUNC(SYSDATE,'MM')                                                             

    -------------------                                                            

    2012-07-01 00:00:00                                                            

    SQL> SELECT TRUNC(sysdate,'dd') FROM dual;(显示当前年月日)

    TRUNC(SYSDATE,'DD')                                                            

    -------------------                                                            

    2012-07-20 00:00:00                                                            

    SQL> SELECT TRUNC(sysdate,'yy') FROM dual;(显示当前年的第一天)

    TRUNC(SYSDATE,'YY')                                                            

    -------------------                                                            

    2012-01-01 00:00:00                                                             

    SQL> SELECT TRUNC(sysdate,'q') FROM dual;(本以为会显示3,结果显示为2012-07-01)

    TRUNC(SYSDATE,'Q')                                                             

    -------------------                                                            

    2012-07-01 00:00:00                                                            

    SQL> SELECT TRUNC(sysdate,'ww') FROM dual;(显示当前周的第一天的日期)

    TRUNC(SYSDATE,'WW')                                                            

    -------------------                                                            

    2012-07-15 00:00:00                                                            

    SQL> SELECT TRUNC(sysdate,'ddd') FROM dual;(显示当前年月日)

    TRUNC(SYSDATE,'DDD'                                                            

    -------------------                                                            

    2012-07-20 00:00:00                                                             

    SQL> SELECT TRUNC(sysdate,'d') FROM dual;(显示当前周的第一天)

    TRUNC(SYSDATE,'D')                                                             

    -------------------                                                            

    2012-07-15 00:00:00                                                            

    SQL> SELECT TRUNC(to_date('2012-07-01','yyyy-mm-dd'),'d') FROM dual; (显示当前周的第一天)

    TRUNC(TO_DATE('2012                                                            

    -------------------                                                            

    2012-07-01 00:00:00                                                            

    SQL> SELECT TRUNC(to_date('2012-07-03','yyyy-mm-dd'),'d') FROM dual; (显示当前周的第一天)

    TRUNC(TO_DATE('2012                                                             

    -------------------                                                            

    2012-07-01 00:00:00                                                            

                                                          

    SQL> SELECT TRUNC(to_date('2012-07-03 08:43:25','yyyy-mm-dd hh24:mi:ss'),'hh') FROM dual;(显示当前时间的年月日和小时,0:00-12:00)

    TRUNC(TO_DATE('2012                                                            

    -------------------                                                             

    2012-07-03 08:00:00                                                            

    SQL> SELECT TRUNC(to_date('2012-07-03 08:43:25','yyyy-mm-dd hh24:mi:ss'),'hh24') FROM dual; (显示当前时间的年月日和小时,0:00-24:00)

    TRUNC(TO_DATE('2012                                                             

    -------------------                                                            

    2012-07-03 08:00:00                                                            

    SQL> SELECT TRUNC(to_date('2012-07-03 08:43:25','yyyy-mm-dd hh24:mi:ss'),'mi') FROM dual;(显示当前时间的年月日和小时,分钟)

    TRUNC(TO_DATE('2012                                                            

    -------------------                                                            

    2012-07-03 08:43:00                             

                                   

    SQL> SELECT TRUNC(to_date('2012-07-03 08:43:25','yyyy-mm-dd hh24:mi:ss'),'ss') FROM dual;(无法精确到秒)

    SELECT TRUNC(to_date('2012-07-03 08:43:25','yyyy-mm-dd hh24:mi:ss'),'ss') FROM dual

                                                                        *

    第 1 行出现错误:

    ORA-01899: 精度说明符错误

    SQL> SELECT TRUNC(to_date('2012-07-03 08:43:25','yyyy-mm-dd hh24:mi:ss'),'sssss') FROM dual; (无法精确到秒)

    SELECT TRUNC(to_date('2012-07-03 08:43:25','yyyy-mm-dd hh24:mi:ss'),'sssss') FROM dual

                                                                        *

    第 1 行出现错误:

    ORA-01899: 精度说明符错误

    SQL> SELECT TRUNC(to_date('2012-07-03 08:43:25','yyyy-mm-dd hh24:mi:ss'),'ddd') FROM dual;(显示当前时间的年月日)

    TRUNC(TO_DATE('2012                                                             

    -------------------                                                            

    2012-07-03 00:00:00                                                            

    SQL> SELECT TRUNC(to_date('2012-07-03 08:43:25','yyyy-mm-dd hh24:mi:ss'),'d') FROM dual;(显示当前周的第一天)

    TRUNC(TO_DATE('2012                                                            

    -------------------                                                            

    2012-07-01 00:00:00                                                            

    SQL> SELECT TRUNC(to_date('2012-07-03 08:43:25','yyyy-mm-dd hh24:mi:ss'),'day') FROM dual; (显示当前周的第一天)

    TRUNC(TO_DATE('2012                                                            

    -------------------                                                            

    2012-07-01 00:00:00                                                            

    SQL> SELECT TRUNC(to_date('2012-07-03 08:43:25','yyyy-mm-dd hh24:mi:ss'),'dd') FROM dual; (显示当天)

    TRUNC(TO_DATE('2012                                                            

    -------------------                                                            

    2012-07-03 00:00:00       

            

    2.TRUNC(for number)

    Oracle TRUNC函数返回处理后的数值,其工作机制与ROUND函数极为类似,只是该函数不对指定小数前或后的部分做相应的四舍五入,而统统截去。

    其具体的语法格式如下

        TRUNC(number[,decimals]) 

    其中:

    number 待做截取处理的数值

    decimals 指明需保留小数点后面的位数。可选项,忽略它则截去所有的小数部分   

    看测试:

    SQL> SELECT TRUNC(3.1415926)FROM dual;

    TRUNC(3.1415926)                                                               

    ----------------                                                               

                   3                                                                

    SQL> SELECT TRUNC(3.1415926,2)FROM dual;

    TRUNC(3.1415926,2)                                                             

    ------------------                                                             

                  3.14                                                              

    SQL> SELECT TRUNC(3.1415926,4)FROM dual;

    TRUNC(3.1415926,4)                                                             

    ------------------                                                             

                3.1415                                                             

    SQL> SELECT TRUNC(3.1415926,-1)FROM dual;

    TRUNC(3.1415926,-1)                                                            

    -------------------                                                             

                      0                                                            

    SQL> SELECT TRUNC(33.1415926,-1)FROM dual;

    TRUNC(33.1415926,-1)                                                           

    --------------------                                                           

                      30           

    注意:第二个参数可以为负数,表示为小数点左边指定位数后面的部分截去,即均以0记     

    摘自:http://www.cnblogs.com/xiaoyudz/archive/2011/03/18/1988467.html

  • 相关阅读:
    解决一起web 页面被劫持的案例
    django rest framwork教程之外键关系和超链接
    django restframwork 教程之authentication权限
    Puppet nginx+passenger模式配置
    Django restframwork教程之类视图(class-based views)
    使用emplace操作
    C++中的显示类型转换
    整数加法
    在不知道学生人数和每个学生课程数量的情况下对学生的平均成绩排序
    树的高度
  • 原文地址:https://www.cnblogs.com/newmanzhang/p/3250240.html
Copyright © 2011-2022 走看看