世界上并没有完美的程序,但我们并不因此而沮丧,因为写程序本来就是一个不断追求完美的过程。 ——摘自周志明

Oracle日期函数

当字段类型为DATE时

1、 在插入数据时需要把字符串转日期类型,通过to_date函数
语法: to_date('2020-10-10','yyyy-mm-dd')
2、 查询日期字段,如果不格式化,默认格式不友好,格式化使用to_char函数
语法: to_char('2020-10-10','yyyy-mm-dd')
3、 trunc()函数

  • 针对日期
    select trunc(sysdate) from dual --2017/6/13  返回当天的日期
    
    select trunc(sysdate,'yyyy') from dual   --2017/1/1  返回当年第一天.
    
    select trunc(sysdate,'mm') from dual  --2017/6/1  返回当月第一天
    
  • 针对数值类型
    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
    

问题:根据生日统计年龄?
floor(months_between(trunc(sysdate,'dd'),birthday))/12),其中birthday为DATE类型。

posted @ 2020-11-30 22:39  白杯与咖啡  阅读(108)  评论(0编辑  收藏  举报