数据库A表中有一个代表年份的列 Year 和一个代表月份的字段 Month假如从程序端传来一个日期 比如 2011/11/02我想查询出所有小于 2011/11/02 的数据

解决方案 »

  1.   

    select * from a where year < substr('2011/11/02',1,4) and month < substr('2011/11/02',6,2);
      

  2.   

    where (year*10000+month) < to_number(to_char(日期参数,'yyyymmdd'))
      

  3.   

    逻辑上有问题吧!
    为什么不用Date数据库类型存储年月呢,郁闷啊。
    因为你数据库中只保存了年和月,但你传过来的数据,还有日,你如何判断你的你的数据是哪日的。要只比较到月,可以这样做:SELECT TO_DATE(年的字段名 || 月的字段名, 'YYYY-Month'),其它字段
    FROM 表名
    WHERE TO_DATE(年的字段名 || 月的字段名, 'YYYY-Month') < TO_DATE('2011-11月', 'YYYY-Month');
      

  4.   

    假设yearcol , monthcol都是数值型,且yearcol都是四位的.select a.* from a where to_char(yearcol) || lpad(to_char(monthcol),2,'0') <= to_char(传来一个日期,'YYYYMM')
      

  5.   

    还想问下 where语句后可以加if吗比如 select * from A where if A.id is not null then A.id=100这么写对不
      

  6.   

    数据库A表中有一个代表年份的列 Year 和一个代表月份的字段 Month
    小于2011/11/02这个做不到的即使我传个2011/11/30与传2011/11/02没什么区别的因为你数据表中没有存day的字段to_date(year||'/'||month,'YYYY/MM') 结果会为year/month/01
      

  7.   

    用case when试试
    select t.*, case when t.id is not null then 100 else t.id end new_id from A t;