本来觉得这问题不难,可是调了半天,总是在报错:
表里有个时间字段,格式:2010-11-02 00:38:23,varchar2类型的。想写个sql查询下2010-11-02这天或是某一天的信息数,一直报错,invalid number。网上看了别人也是这么写的,都说没问题。
select * from t_acsrecord where to_char(accesstime,'yyyy-mm-dd') = to_char(sysdate,'yyyy-mm-dd')
(ORA-01722:invalid number)
select * from t_acsrecord where trunc(accesstime) = trunc(sysdate)
(ORA-00932:inconsistent datatypes:expected NUMBER got DATE)应该是类型不匹配吧,哪位高人支支招啊

解决方案 »

  1.   

    accesstime 字段是什么类型的? date ?
      

  2.   

    可以这样:select * from t_acsrecord 
    where 
    to_date(accesstime,'yyyy-mm-dd hh24:mi:ss') = 
    to_date(sysdate,'yyyy-mm-dd hh24:mi:ss')或者这样:select * from t_acsrecord
    where
    trunc(to_date(accesstime,'yyyy-mm-dd hh24:mi:ss')) = trunc(sysdate)
      

  3.   

    where trunc(sysdate,'dd')=to_date(accesstime,'yyyy-mm-dd')
      

  4.   

    还是有错ORA-01861:literal does not match format string
      

  5.   

    select * from t_acsrecord 
    where 
    trunc(to_date(accesstime,'yyyy-mm-dd hh24:mi:ss'),'dd') = 
    trunc(sysdate,'dd')
      

  6.   

    varchar2 类型,就不需要 to_char(accesstime) 了,直接 accesstime = to_char(sysdate,'yyyy-mm-dd')
      

  7.   

    恩,确实是,但是accesstime还是要截取过的,不然取不到数据了
      

  8.   

    lz说的对,效率很重要.
    如果accesstime列上建了索引的话使用函数会使索引失效.所以可以使用下面的方式查询
    SELECT *
      FROM t_acsrecord
     WHERE accesstime >= TO_CHAR (TRUNC (SYSDATE), 'yyyy-mm-dd hh24:mi:ss')
       AND accesstime < TO_CHAR (TRUNC (SYSDATE + 1), 'yyyy-mm-dd hh24:mi:ss')
      

  9.   

    恩,其实还要做一个整张表数量的查询,数据量打的话count(*)好像效率很低,网上有说建位图索引,看来要用您的方法试试看了
      

  10.   

    额,发现个奇怪的问题,在PLSQL工具里面测试了下,语句没问题。不过放在eclipse IDE下时,那个小于号竟然报错了,大于等于倒是没问题
      

  11.   

    不关sql语句的是,是ibatis的问题,我是小白,大大们莫怪啊