有个time字段,类型varchar,用来表示时间,格式为 yyyymmdd。
现在要查三天以内的记录,就是说,time要大于等于三天前的日期。怎么写好?只有50分了,都给了.
select * from mytable where time?????

解决方案 »

  1.   

    select * from mytable
    where datediff(day,cast(time as datetime),getdate())>=3
      

  2.   

    select * from mytable where time<convert(varchar(10),getdate()-3,120)
      

  3.   

    select * from tb where datediff(convert(datetime,[time],112),getdate())<=3
      

  4.   

    楼上的貌似需要修改下SELECT * FROM tb WHERE DATEDIFF(DAY,CONVERT(datetime,[time],112),getdate())<=3
      

  5.   

    thank you~!
    再问一下,什么数据库都可以用吗?
      

  6.   

    --sql server
    select * from tb where datediff(day , time , getdate()) <= 3--oracle
    select * from tb where ceil(sysdate - To_date(time, 'yyyy-mm-dd hh24-mi-ss')) <= 3