使用Oracle数据库,mybatis的映射文件中日期类型的数据,如果定义为<result column =“UPDATED_DATE" jdbcType =“DATE” property =“updatedDate”/>,即使在后台你为日期赋予年月日时分秒,但保存到数据库中将只会有年月日,而无时分秒。如果要保存为年月日时分秒,日期类型的需要定义为jdbcType为 “timdstamp”
另外在PL SQL中通过SQL语句往oracle数据库添加日期类型的数据方法为:
insert into table(j) values(to_date(‘2017-11-26 00:04:47’,‘yyyy-mm-dd hh24:mi:ss’));
即不能直接为为字段赋值,需要通过to_date函数,后面接日期格式,修改也是一样的:
update table t set t.updated_date=to_date(‘2018-10-11 08:15:16’,‘yyyy-mm-dd hh24:mi:ss’) where t.id=‘2152928’;
查询当天数据的sql,在mybatis中可以这样使用:select * from table where to_char(UPDATED_DATE,‘yyyy-mm-dd’)=to_char(sysdate,‘yyyy-mm-dd’) 在PL SQL中可以这样用:select * from table where trunc(UPDATED_DATE)=trunc(sysdate)
下面是常用日期作为条件的查询SQL
1.日期字段类型为date
今天
select * from table where to_char(UPDATED_DATE,'dd')=to_char(sysdate,'dd')
昨天
select * from table where to_char(UPDATED_DATE,'dd')= to_char(sysdate-1,'dd')
本周
select * from table where to_char(UPDATED_DATE,'iw')=to_char(sysdate,'iw')
本月
select * from table where to_char(UPDATED_DATE,'mm')=to_char(sysdate,'mm')
本季度
select * from table where to_char(UPDATED_DATE,'q')=to_char(sysdate,'q')
-
日期字段类型为varchar2,格式要与格式化的样式匹配
今天
select * from table where to_char(to_date(UPDATED_DATE,‘yyyy-mm-dd hh24:mi:ss’),‘dd’)=to_char(sysdate,‘dd’)
昨天
select * from table where to_char(to_date(UPDATED_DATE,‘yyyy-mm-dd hh24:mi:ss’),‘dd’)=to_char(sysdate-1,‘dd’)
本周
select * from table where to_char(to_date(UPDATED_DATE,‘yyyy-mm-dd hh24:mi:ss’),‘iw’)=to_char(sysdate,‘iw’)
本月
select * from table where to_char(to_date(UPDATED_DATE,‘yyyy-mm-dd hh24:mi:ss’),‘mm’)=to_char(sysdate,‘mm’)
本季度
select * from table where to_char(to_date(UPDATED_DATE,‘yyyy-mm-dd hh24:mi:ss’),‘q’)=to_char(sysdate,‘q’)