比如我选08:18:00   现在在数据库里面查找08:17:00到08:18:59  中那个时间点离08:18:00最近···  表名是gpsdata20121219  时间的字段是time  类型是varchar2 大神们 帮帮我吧 oracle数据库

解决方案 »

  1.   

    进行差值比较即可:
    select case when 时间1-选择时间>时间2-选择时间 then 时间2 else 时间1 from gpsdata20121219 ;
      

  2.   

    select case when 时间1-选择时间>时间2-选择时间 then 时间2 else 时间1 end from gpsdata20121219 ;
    少了end
      

  3.   


    select case when (To_date('2012-12-19 08:17:00','yyyy-mm-dd hh24-mi-ss'))-(To_date('2012-12-19 08:18:00','yyyy-mm-dd hh24-mi-ss'))>(To_date('2012-12-19 08:18:59','yyyy-mm-dd hh24-mi-ss'))-(To_date('2012-12-19 08:18:00','yyyy-mm-dd hh24-mi-ss')) then '2012-12-19 08:18:59' else '2012-12-19 08:17:00' end from gpsdata20121219 ;
    我这样写怎么一直循环了
      

  4.   

    select case
             when (To_date('20121219081700', 'yyyymmddhh24miss')) -
                  (To_date('20121219081800', 'yyyymmddhh24miss')) >
                  (To_date('20121219081859', 'yyyymmddhh24miss')) -
                  (To_date('20121219081800', 'yyyymmddhh24miss')) then
              '2012-12-19 08:18:59'
             else
              '2012-12-19 08:17:00'
           end
      from dual
      
      

  5.   

     with A as ( select to_date('2013-01-01','yyyy-mm-dd') as times from dual 
                 union all 
                 select to_date('2013-03-01','yyyy-mm-dd') as times from dual   
                 union all 
                 select to_date('2013-07-01','yyyy-mm-dd') as times from dual)
         select * from (
            select   to_date('2013-02-01','yyyy-mm-dd') - times  时间差,a.* from A order by abs(时间差) asc ) where rownum =1
           
      

  6.   

    haing min(abs(datecol -userdate)) 获取最小的差异值,再求出该查差异值对应的数据
      

  7.   

    select * from tab t
    where abs(t.datecol -userdate) = (
    select min(abs(t1.datecol -userdate))
    from tab t1
    having min(abs(t1.datecol -userdate))
    )
      

  8.   

                select * from gpsdata20121219 t
    where abs(to_date(t.time...) -To_date('20121219081700', 'yyyymmddhh24miss')) = (
    select min(abs(to_date(t1.time...) -To_date('20121219081700', 'yyyymmddhh24miss')))
    from gpsdata20121219 t1
    having min(abs(to_date(t1.time...)  -To_date('20121219081700', 'yyyymmddhh24miss')))
    )   
    to_date(t1.time...)  是time转换为datetime的方法,我很少用,自己补充一下