• 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

  • 相关阅读:
    SSAS的维度表之间的关系只能有一个不能有多个
    SqlServer 在创建数据库时候指定的初始数据库大小是不能被收缩的
    SQL Server数据库的三种恢复模式:简单恢复模式、完整恢复模式和大容量日志恢复模式(转载)
    HttpHandler和ashx要实现IRequiresSessionState接口才能访问Session信息(转载)
    Jquery Ajax调用aspx页面方法 (转载)
    linux查找目录下的所有文件中是否含有某个字符串 <zhuan>
    ubuntu下使用sdk manager 安装sdk 其他版本
    Ubuntu更新命令 <转>
    sudo:must be setuid root 解决方法 <转>
    Ubuntu 查看磁盘空间大小命令<转>
  • 原文地址:https://www.cnblogs.com/newmanzhang/p/3250240.html
Copyright © 2020-2023  润新知