select * from t_goods_decl t where t.decl_date like '%'||(to_char(sysdate,'yyyy')||'-6')||'%'
我这样写却一条记录都查找不出来,但是日期像'2011-6'的记录还是不少的,是什么问题呢?

解决方案 »

  1.   

    -- decl_date 这个字段不是字符类型的吧?你查看一下这个字段的类型,应该是日期类型的
    -- 如果是日期类型的话,最好这样写:select * from t_goods_decl t
    where t.decl_date>=to_date('2011-06-01','yyyy-mm-dd') 
    and t.decl_date<to_date('2011-07-01','yyyy-mm-dd') 
      

  2.   

    -- 记住:只有字符串类型(CHAR、VARCHAR、VARCHAR2、CLOB类型等),才能用 like 模糊查询!
      

  3.   

    你的decl_date是date类型的话,直接like是不行的如果是日期型,最好这样比较
    select * from t_goods_decl t where t.decl_date>=date'2011-06-01 and t.decl_date<date'2011-07-01';
    如果列decl_date上有索引,才会利用上索引
      

  4.   

    没错,它是日期类型来的,
    但是,我先把它转成字符类型也不行啊select * from t_goods_decl t where to_char(t.decl_date,'yyyy-mm-dd') 
    like '%'||(to_char(sysdate,'yyyy')||'-6')||'%'
    还是查找不出来.
      

  5.   

    没错,它是日期类型来的,
    但是,我先把它转成字符类型也不行啊select * from t_goods_decl t where to_char(t.decl_date,'yyyy-mm-dd') 
    like '%'||(to_char(sysdate,'yyyy')||'-06')||'%'-6 修改为 -06
      

  6.   

    你这个to_char(t.decl_date,'yyyy-mm-dd')转换出来是'2011-06-01' like '%2011-6%'加上0
    select * from t_goods_decl t where to_char(t.decl_date,'yyyy-mm-dd') 
    like '%'||(to_char(sysdate,'yyyy')||'-06')||'%'
      

  7.   


    -- 其实这是低效的写法,具体示例请看:
    http://topic.csdn.net/u/20110323/13/43a9766c-9a40-470d-8adb-9c2594282203.html
      

  8.   

    你这个模糊匹配不就是想要查询出2011年6月份的数据嘛!
    不要使用模糊匹配:
    select * from t_goods_decl t 
    where t.decl_date >= to_date('2011-06-01','yyyy-mm-dd')
      and t.decl_date < to_date('2011-07-01','yyyy-mm-dd');
    如果想上面你的那么写:
    where to_char(t.decl_date,'yyyy-mm-dd')  
    like '%'||(to_char(sysdate,'yyyy')||'-6')||'%'
    decl_date列上有索引,查询时是不会走索引的
      

  9.   

    方法三也用到了trunc函数啊,怎么效率会高那么多呢?不明白
    还有索引范围扫描是什么概念呢