zoukankan      html  css  js  c++  java
  • oracle笔记之计算年龄、工龄和TRUNC

    方法一:利用months_between 函数计算

    SELECT TRUNC(months_between(sysdate, birthday)/12) AS age
    from dual;


    方法二:日期转换为 'yyyyMMdd' 格式后,相差一年的两个日期差为:10000,缺点是只能精确到年,并且不能四舍五入。
    select TRUNC((to_char(sysdate, 'yyyyMMdd') - to_char(birth, 'yyyyMMdd')) /
    10000) as age
    from mytable
     

    /**************TRUNC之日期********************/

    select trunc(sysdate) from dual ;--2011-3-18 今天的日期为2011-3-18
    select trunc(sysdate, 'mm') from dual ; --2011-3-1 返回当月第一天.
    select trunc(sysdate,'yy') from dual; --2011-1-1 返回当年第一天
    select trunc(sysdate,'dd') from dual; --2011-3-18 返回当前年月日
    select trunc(sysdate,'yyyy') from dual; --2011-1-1 返回当年第一天
    select add_months(to_date('2018-01-01','yyyy-mm-dd'),12)-1 from dual; -- 2018-12-31 返回当年第一天
    select trunc(sysdate,'d') from dual ; --2011-3-13 (星期天)返回当前星期的第一天
    select trunc(sysdate, 'hh') from dual ; --2011-3-18 14:00:00 当前时间为14:41
    select trunc(sysdate, 'mi') from dual ; --2011-3-18 14:41:00 TRUNC()函数没有秒的精确
    SELECT to_date('20180818','yyyymmdd')-to_date('20180727','yyyymmdd') FROM dual;


    /***************TRUNC之数字********************/
    /*
    TRUNC(number,num_digits)
    Number 需要截尾取整的数字。
    Num_digits 用于指定取整精度的数字。Num_digits 的默认值为 0。
    TRUNC()函数截取时不进行四舍五入
    */
    select trunc(123.458) from dual; --123
    select trunc(123.458,0) from dual ;--123
    select trunc(123.458,1) from dual ;--123.4
    select trunc(123.458,-1) from dual; --120
    select trunc(123.458,-4) from dual; --0
    select trunc(123.458,4) from dual ;--123.458
    select trunc(123) from dual ;--123
    select trunc(123,1) from dual ;--123
    select trunc(123,-1) from dual ;--120

      

  • 相关阅读:
    DataAnnotations
    使用BizTalk实现RosettaNet B2B So Easy
    biztalk rosettanet 自定义 pip code
    Debatching(Splitting) XML Message in Orchestration using DefaultPipeline
    Modifying namespace in XML document programmatically
    IIS各个版本中你需要知道的那些事儿
    关于IHttpModule的相关知识总结
    开发设计的一些思想总结
    《ASP.NET SignalR系列》第五课 在MVC中使用SignalR
    《ASP.NET SignalR系列》第四课 SignalR自托管(不用IIS)
  • 原文地址:https://www.cnblogs.com/Bokeyan/p/11582661.html
Copyright © 2011-2022 走看看