我的数据库设计是:有一个日期的字段“myDate”是字符型,存储格式是:“2009-11-18”,还有一个字段是“myTime”也是字符型,存储格式是:“01:00:00”,每个小时一条数据,是24小时制的。
     现在的问题是:我想查询一段日期,一段时间的数据。例如:日期2009-11-01到2009-11-18;时间20:00:00到02:00:00,这样的SQL语句怎么写。请不吝指教!

解决方案 »

  1.   

    select * from table1
    where datecol||timecol between '2009-11-01' and '2009-11-18'
      and (timecol>='20:00:00' or timecol<='02:00:00')
      

  2.   

    ..
    select * from table1 
    where datecol between '2009-11-01' and '2009-11-18' 
      and (timecol>='20:00:00' or timecol <='02:00:00')
      

  3.   

    怎么不把 myDate和myTime合并,存储为date格式呢?有特殊需要?
    如果是直接date between to_date('2009-11-01 20:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('2009-11-18 02:00:00','yyyy-mm-dd hh24:mi:ss')现在可以 to_date(myDate||' '||myTime,'yyyy-mm-dd hh24:mi:ss') between to_date('2009-11-01 20:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('2009-11-18 02:00:00','yyyy-mm-dd hh24:mi:ss')
      

  4.   

    select * from table where  to_date(myDate||' '||myTime,'yyyy-mm-dd hh24:mi:ss') between to_date('2009-11-01 20:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('2009-11-18 02:00:00','yyyy-mm-dd hh24:mi:ss') 
      

  5.   

    二楼的方法是不行的,因为如果我是查询日期2009-11-01到2009-11-01;时间02:00:00到08:00:00这段时间的数据的话,那应该就是
    datecol between '2009-11-01' and '2009-11-01' 
      and timecol>='02:00:00' and timecol <='08:00:00'
      

  6.   

    呵呵 对呀
    以24点为界限的
    根据你要求的时段不同使用的连接词也就不同了
    一个用or  一个用and
      

  7.   

    select * from table where myDate between to_date('2009-11-01 20:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('2009-11-18 02:00:00','yyyy-mm-dd hh24:mi:ss') ;