字符串在Oracle数据库中的转换问题

来源:互联网 发布:专业点餐软件 编辑:程序博客网 时间:2024/06/05 16:42

我现在项目用的oracle数据库,由于在表中储存的是timestamp类型,我要查询在一定的时间范围内的值,写了如下的代码:

//日期的写法;
if(StringUtils.isNotEmpty(dto.getStartTime())){
wheresql+=(" and to_char(dto.startTime,'yyyy-MM-dd hh:mm:ss')>=:StartTime");//此处是字符串比较
String time=dto.getStartTime()+" 00:00:00";
map.put("StartTime",time);
}

由于是要做时间的比较,目前直接从数据中取出的值是经过hibernate映射过的,此时的time已经是String类型了,而如果直接用timestamp类型与String是无法比较的。现在比较时,必须得两边统一类型,要么比较字符串,或者是比较Date类型,我用的是前者,比较字符串,所以就要到了oracle数据库专门提供的to_char( )函数。

如果两边比较Date类型,那就用到Oracle提供的to_date( )函数。

例如:

public List queryOperLogList(OperLog ol,String start,String end) {

StringBuffer hql = new StringBuffer("from OperLog dto  where 1=1   " );
if(!StringUtil.nullToStr(ol.getBusiCode()).equals("")){
hql.append(" and dto.busiCode in ("+ol.getBusiCode()+")");
   }
if(start!=null)
   {
hql.append("and dto.operDate>=  to_date('"+start+"','yyyy-mm-dd hh24:mi:ss') ");

}


0 0
原创粉丝点击