Oracle中的日期运算
来源:互联网 发布:直播平台系统源码 编辑:程序博客网 时间:2024/06/08 12:59
**
一、加减日、月、年
**
在Oracle中,date类型可以直接到加减天数,而加减月份需要使用add_months函数
示例:
SQL> select sysdate as sys_date,---------系统日期 2 sysdate - 2 as "date-2-days",--------减两天 3 sysdate + 2 as "date+2-days",--------加两天 4 add_months(sysdate,-2) as "date-2-months",-------------减两个月 5 add_months(sysdate,+2) as "date+2-months",-------------加两个月 6 add_months(sysdate,-2*12) as "date-2-years",-----------减两年 7 add_months(sysdate,+2*12) as "date+2-years"------------加两年 8 from dual;SYS_DATE date-2-da date+2-da date-2-mo date+2-mo date-2-ye date+2-ye--------- --------- --------- --------- --------- --------- ---------02-OCT-17 30-SEP-17 04-OCT-17 02-AUG-17 02-DEC-17 02-OCT-15 02-OCT-19SQL>
注意:在Oracle中,如果别名有特殊字符,一定要使用双引号引起来,不然会提示错误
**
加减时、分、秒
**
上面的示例中可以看到,date可以直接加减天数,那么1/24就是一小时,分钟与秒的加减类似
由于我的数据库字符集不支持中文,所以在这里,我使用PL/SQLdeveloper来做示例:
SQL> select sysdate as 系统日期, 2 sysdate - 2/24/60/60 as 减2秒, 3 sysdate + 2/24/60/60 as 加2秒, 4 sysdate - 2/24/60 as 减两分, 5 sysdate + 2/24/60 as 加2分, 6 sysdate - 2/24 as 减2小时, 7 sysdate + 2/24 as 加2小时 8 from dual; 系统日期 减2秒 加2秒 减两分 加2分 减2小时 加2小时-------------------- -------------------- -------------------- -------------------- -------------------- -------------------- --------------------2017/9/19 23:06:13 2017/9/19 23:06:11 2017/9/19 23:06:15 2017/9/19 23:04:13 2017/9/19 23:08:13 2017/9/19 21:06:13 2017/9/20 1:06:13
**
日期间隔之时、分、秒
**
两个日期相减,得到的就是天数,乘以24小时,就是小时,以此类推,得到的就是秒
示例:
SQL> select gap_time, 2 gap_time * 24 as gap_hours, 3 gap_time * 24 * 60 as gap_min, 4 gap_time * 24 * 60 * 60 as gap_sed 5 from 6 (select sysdate - max(hiredate) as gap_time 7 from scott.emp); GAP_TIME GAP_HOURS GAP_MIN GAP_SED---------- ---------- ---------- ----------11078.0304 265872.729 15952363.7 957141825SQL>
间隔日、月、年
加减月份使用add_months函数,而计算月份间隔则需要使用months_between函数
示例:计算emp表中雇佣时间最长的员工到现在间隔的天数、月数和年数
SQL> select sysdate-min_hd as gap_days, 2 months_between(sysdate,min_hd) as gap_months, 3 months_between(sysdate,min_hd) / 12 as gap_years 4 from 5 (select sysdate,min(hiredate) as min_hd from scott.emp); GAP_DAYS GAP_MONTHS GAP_YEARS---------- ---------- ----------13426.0349 441.0979 36.7581583SQL>
以上就是关于时间的基本运算。
阅读全文
0 0
- Oracle中的日期运算
- oracle日期运算
- Oracle 日期加减运算
- Oracle 日期运算1
- Oracle 日期运算2
- Oracle之日期运算
- Oracle 日期加减运算
- oracle中的日期问题
- Oracle中的日期格式
- oracle中的日期【转】
- ORACLE中的日期格式
- Oracle中的日期时间
- oracle中的日期
- oracle中的日期格式
- oracle 中的日期函数
- oracle 中的日期函数
- oracle中的日期选择
- Oracle日期运算,你知多少?
- 导航栏制作
- 序列化的作用
- 系统工具
- 堆排序
- 中位数计数
- Oracle中的日期运算
- 用户管理及权力下放命令
- 最新版本微信和QQ红包自动领取安装包
- HDU
- 01-复杂度2 Maximum Subsequence Sum(25 分)
- git stash 恢复
- spring-retry注解方式使用(断路器,重试)
- 关于制作游戏中产生的子弹方向问题的思考
- 【20171003】python_语言设计(5)字典