方法一:利用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

最新文章

  1. Daily Scrum02 12.16
  2. Implementation Model Editor of AVEVA in OpenSceneGraph
  3. 【大数据】Summingbird(Storm + Hadoop)的demo运行
  4. 这些web前端特效你造吗?
  5. Servlet乱码
  6. [转]Windows配置Git
  7. DB2中ixf文件的导入导出
  8. ado无法访问数据库问题
  9. 关于重写ID3 Algorithm Based On MapReduceV1/C++/Streaming的一些心得体会
  10. 170113、CentOs6.4中安装和配置vsftp简明教程
  11. Django项目实战之用户上传与访问
  12. SVM及其c++代码运用实例
  13. Struts2 中的数据传输的几种方式
  14. vue $refs 无法动态拼接,获取不到对象(转)
  15. # 【Python3练习题 008】判断101-200之间有多少个素数,并输出所有素数。
  16. 26、jQuery
  17. springboot 接收post和get请求
  18. Vue源码翻译之渲染逻辑链
  19. POJ 1269 - Intersecting Lines - [平面几何模板题]
  20. 在Linode VPS上搭建最新版Transmission

热门文章

  1. Qualcomm Audio HAL 音频通路设置【转】
  2. ArcPy python实例教程-条件平差-测量平差
  3. vue子组件数据变化同步到父组件中
  4. Thread.currentThread与this的区别
  5. flutter chip标签组件
  6. openresty开发系列21--lua的模块
  7. jenkins下载插件Git Parameter插件
  8. SQLite带参数处理方法
  9. 浏览器显示 req Provisional headers are shown
  10. 【翻译】Flink Joining