oracle中date类型能比较大小吗?

WHERE (OP.RIQI >= '2011-06-01')

AND (OP.RIQI < = '2011-07-26')) T1这个写法正确吗?如果正确是不是说时间类型可以比较大小?

可以比较,具体比较方法如下:

[java] view plain copy

    Service:  

    String hql = "SELECT COUNT(*) FROM Instructions  ";  

    hql =hql+where;  


    [java] view plain copy

    String strStartDate=ParamUtil.getString(request,"strStartDate","");//格式为:2010-05-03  

    String strEndDate=ParamUtil.getString(request,"strEndDate","");//格式为:2016-06-01  

    if(!strStartDate.equals("")){  

    tWhere+=" and dtCreatDate>=to_date('"+strStartDate+" 00:00:00','yyyy-mm-dd hh24:mi:ss')";  

    }  

    if(!strEndDate.equals("")){  

    tWhere+=" and dtCreatDate<=to_date('"+strEndDate+" 23:59:59','yyyy-mm-dd hh24:mi:ss')";  

    }  

    [java] view plain copy

    int count = objSvr.getCount(tWhere);  

    打印的语句如下:

    [sql] view plain copy

    WHERE intVirDel<>1  and dtCreatDate>=to_date('2016-06-01 00:00:00','yyyy-mm-dd hh24:mi:ss') and dtCreatDate<=to_date('2016-06-24 23:59:59','yyyy-mm-dd hh24:mi:ss')  

时间类型可以比较大小,但是日期格式需要转成字符串,或者字符串转成日期来比较

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-25
时间类型可以比较大小,但是日期格式需要转成字符串,或者字符串转成日期来比较

比如你的这个

WHERE (to_char(OP.RIQI,'yyyy-mm-dd') >= '2011-06-01')

AND (to_char(OP.RIQI,'yyyy-mm-dd') < = '2011-07-26'))

或者
WHERE (OP.RIQI >= to_date('2011-06-01','yyyy-mm-dd'))

AND (OP.RIQI < = to_date('2011-07-26','yyyy-mm-dd'))追问

你写的这个我可以理解,但是这是我在资料上看到的,就是这样写的,OP.RIQI这样会有执行结果吗?

追答

恩,日期和字符都可以比较大小,保证没问题的,只要你数据在那个区间呢,肯定有结果的

否则像你那么写,很有可能会报错的

本回答被提问者采纳
第2个回答  2013-09-25
时间类型可以比较大小,但你这个不是时间类型,得用得做转化
比如op.riri是date类型的,你可以op.riqi > to_date('2011-07-26','yyyy-mm-dd')追问

这是我在资料上看到的,就是这样写的,OP.RIQI这样会有执行结果吗?

追答

OP.RIQI 这个在数据库里是什么类型的呢? 如果是date类型的那应该会报错的

第3个回答  2013-09-25
可以比较大小,譬如:select * from GC_GARBAGE t where t.submittime < to_date( '2012-12-13 ' , ' yyyy-mm-dd ' );
select * from GC_GARBAGE t where t.submittime >to_date( '2012-12-13 10:58:00' , ' yyyy-mm-dd hh24:mi:ss' );
第4个回答  2013-09-25
都转换成字符串或者时间类型就能比较。