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类型。

原文地址:https://www.cnblogs.com/xingrui/p/14064371.html