`

Oracle日期函数

    博客分类:
  • Sql
 
阅读更多
获取当前日期 sysdate:to_char(sysdate,'dd-mm-yyyy day')
001、
日期格式(以时间:2007-11-02  周五 13:45:25 为例)
年:
    YY        两位: 07         
    YYYY    四位:2007
月:
    MM:        11
    MON:         11(中文版)    nov(英文版)
    MONTH:    11月(中文版)    november
天:
    D:        ?    周内第几天(范围周日1--------周六7) 
    DD:      02    当月第几天
    DDD:    ?    当年第几天
    DY:        星期五(中文版)    fri(英文版)
    DAY:      星期五(中文版)    friday(英文版)   
时:
    HH:         01    12小时制
    HH24:    13    24小时制   
分:
    MI:    45
秒:
    SS:    25
季度:
    Q:    4    范围(1-----4)
周数:
    WW:    44    年度周数
    W:       1       月度周数
002、
字符-------时间
to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') 
to_char(sysdate,'yyyy') 
to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss')
to_date('2004-05-07','yyyy-mm-dd')
to_date('2004-05-07 13:23:44','yyyy-mm-dd')  模式不匹配,错误
to_timestamp('2008-11-17 00:31:35', 'YYYY-MM-DD HH24:MI:SS:FF')
 

003、

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 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()函数没有秒的精确
    /***************数字********************/
    /*
    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
 

004、

计算时间差
      注:oracle时间差是以天数为单位,所以换算成年月,日    
    //时间差-年  
  select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))/365)
    //时间差-月
      select ceil(moths_between(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss')))  
    //时间差-天
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss')))
     //时间差-时
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24) 
     //时间差-分
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60)
    //时间差-秒
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60*60)
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics