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