Mybatis连接Oracle时间段筛选

1、如果 oracle 数据库中存的 time 是类型是 varchar2 ,传入参数也是字符串。

   

① select * from table where time < #{time}

      

或者是 ② select * from table where time &le; #{time}

      

在或者是 ③ select * from table where time <![CDATA[ < ]] #{time}

   

这3条sql的意义都一样,但是<![CDATA[ ]]中的字符 会保证 [] 中的字符不会被转义,xml中建议使用②③。

   

   

2、比较时间字符串格式 和 时间格式。如果筛选条件对带索引字段的进行格式转换,索引会失效。

 

转化成相同类型的比较,统一格式。

   

使用to_char(prarm ,"yyyy-mm-dd 24HH:mi:ss") 转成字符串比较

   

select * from to_char(createTime , 'yyyy-mm-dd 24HH:mi:ss') < "2020-10-08 23:23:23" ;

   

   

使用to_date(prarm ,"yyyy-mm-dd 24HH:mi:ss") 转成时间比较

   

select * from createTime < to_date('2020-10-08 23:23:23', 'yyyy-mm-dd 24HH:mi:ss') ;

   

   

3、关于to_date()函数

在oracle中建有date类型的字段,插入可以采取如下方法:

如果是小时为:1-12采取如下格式:yyyy-mm-dd HH:MI:SS

insert into test values(to_date('2020-10-08 23:23:23','yyyy-mm-dd HH:MI:SS')) ;

   

如果是小时为:1-24采取如下格式:yyyy-mm-dd HH24:MI:SS

insert into test values(to_date('2020-10-08 23:23:23','yyyy-mm-dd HH24:MI:SS')) ;

   

   

insert into table(createTime) value(to_date('2020-10-08','yyyy-mm-dd')) ;

   

insert into table(createTime) value(to_date('2020-10-08 23:23:23','yyyy-mm-dd hh24:mi:ss')) ;

   

insert into table(createTime) value(to_date('20201008','yyyymmdd')) ;

   

insert into table(createTime) value(to_date('20201008232323','yyyymmddhh24miss')) ;

posted @ 2020-12-05 16:19  黑质白章  阅读(2334)  评论(0编辑  收藏  举报