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')) ;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM