oracle中常用的 SQL语句中进行时间加减
一:
表明x的单位,这个函数把x转为interval day to second数据类型
-----------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.cnpc.oms2.qual.apply.mapper.QualApplyExternalUserMapper"> <select id="showExternalUser" parameterType="hashmap" resultType="hashmap"> SELECT exUser.PK_USEREXTEND_CHANGE_ID, exUser.EMPLOYEE_ID, exUser.ENT_NAME, exUser.NAME, exUser.SERVER_OILFIELD, exUser.REQBOOK_NUM, exUser.IS_VALID, exUser.BSFLAG, exUser.CREATE_USER, exUser.UPDATE_USER, exUser.CREATE_DATE, exUser.UPDATE_DATE, exUser.ORG_CODE, sysUser.USER_NAME as USER_NAME, sysUser.START_DATE+numtodsinterval(sysUser.VALID_DATES,'day') as END_DATE FROM QUAL_BASE_USEREXTEND_CHANGE exUser LEFT JOIN OMS_SYS_USER sysUser ON exUser.EMPLOYEE_ID = sysUser.EMPLOYEE_ID <where> <if test="entName !=null and entName != '' "> exUser.ENT_NAME like CONCAT(CONCAT('%', #{entName}),'%') </if> <if test="userName !=null and userName != '' "> AND sysUser.USER_NAME like CONCAT(CONCAT('%', #{userName}),'%') </if> <if test="loginName !=null and loginName != '' "> AND exUser.NAME like CONCAT(CONCAT('%', #{loginName}),'%') </if> <if test="reportUnit !=null and reportUnit != '' "> AND exUser.SERVER_OILFIELD like CONCAT(CONCAT('%', #{reportUnit}),'%') </if> <if test="reportNum !=null and reportNum != '' "> AND exUser.REQBOOK_NUM like CONCAT(CONCAT('%', #{reportNum}),'%') </if> <if test="startDate !=null and startDate !=''"> AND TO_CHAR(sysUser.START_DATE, 'YYYY-mm-dd') >= #{startDate} </if> <if test="endDate !=null and endDate !=''"> AND TO_CHAR(sysUser.START_DATE+numtodsinterval(sysUser.VALID_DATES,'day'), 'YYYY-mm-dd') <= #{endDate} </if> AND exUser.ORG_CODE like CONCAT(CONCAT('%', #{orgCode}),'%') <if test="entId !=null and entId != '' "> OR a.ENT_ID like CONCAT(CONCAT('%', #{entId}),'%') </if> AND exUser.BSFLAG = '0' </where> ORDER BY <if test="isSort == 'yes'"> <trim suffixOverrides=","> </trim> </if> <if test="isSort == 'no'"> END_DATE DESC,exUser.ENT_NAME,exUser.NAME,exUser.SERVER_OILFIELD </if> </select> </mapper>
select sysdate,add_months(sysdate,12) from dual; --加1年
select sysdate,add_months(sysdate,1) from dual; --加1月
select sysdate,to_char(sysdate+7,'yyyy-mm-dd HH24:MI:SS') from dual; --加1星期
select sysdate,to_char(sysdate+1,'yyyy-mm-dd HH24:MI:SS') from dual; --加1天
select sysdate,to_char(sysdate+1/24,'yyyy-mm-dd HH24:MI:SS') from dual; --加1小时
select sysdate,to_char(sysdate+1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual; --加1分钟
select sysdate,to_char(sysdate+1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; --加1秒
减法
select sysdate,add_months(sysdate,-12) from dual; --减1年
select sysdate,add_months(sysdate,-1) from dual; --减1月
select sysdate,to_char(sysdate-7,'yyyy-mm-dd HH24:MI:SS') from dual; --减1星期
select sysdate,to_char(sysdate-1,'yyyy-mm-dd HH24:MI:SS') from dual; --减1天
select sysdate,to_char(sysdate-1/24,'yyyy-mm-dd HH24:MI:SS') from dual; --减1小时
select sysdate,to_char(sysdate-1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual; --减1分钟
select sysdate,to_char(sysdate-1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; --减1秒
SQL> select sysdate, sysdate+numtodsinterval(1,’hour’) from dual ;
SYSDATE SYSDATE+NUMTODSINTE
——————- ——————-
2010-10-14 21:38:19 2010-10-14 22:38:19
对当前日期增加50分种
SQL> select sysdate, sysdate+numtodsinterval(50,’minute’) from dual ;
SYSDATE SYSDATE+NUMTODSINTE
——————- ——————-
2010-10-14 21:39:12 2010-10-14 22:29:12
对当前日期增加45秒
SQL> select sysdate, sysdate+numtodsinterval(45,’second’) from dual ;
SYSDATE SYSDATE+NUMTODSINTE
——————- ——————-
2010-10-14 21:40:06 2010-10-14 21:40:51
对当前日期增加3天
SQL> select sysdate, sysdate+3 from dual ;
SYSDATE SYSDATE+3
——————- ——————-
2010-10-14 21:40:46 2010-10-17 21:40:46
对当前日期增加4个月
SQL> select sysdate, add_months(sysdate,4) from dual ;
SYSDATE ADD_MONTHS(SYSDATE,
——————- ——————-
2010-10-14 21:41:43 2011-02-14 21:41:43
当前日期增加2年
SQL> select sysdate, add_months(sysdate,12*2) from dual ;
SYSDATE ADD_MONTHS(SYSDATE,
——————- ——————-
2010-10-14 21:42:17 2012-10-14 21:42:17
timestamp的操作方法与上面类似;
求两个日期之差:
例:求2007-5-23 21:23:34与当前时间之间的差值。
SQL> select sysdate-to_date(’20070523 21:23:34′,’yyyy-mm-dd hh24:mi:ss’) dt from
dual ;