RT
datetime型字段col,想查当天数据或者昨天数据如何利用col上的索引?

解决方案 »

  1.   

    select id from t where num=@num
    可以改为强制查询使用索引:
    select id from t with(index(索引名)) where num=@num
      

  2.   

    2008 可以用 date 数据类型,就用不着想后面的时间问题了.
      

  3.   

    --比如col上的索引是datetime时创建的
    --那么用到时如果是
    select col from tb with(index=索引) where convert(varchar(10),col,120)='2011-09-02'
    --那这个索引还有用么?起的到检索效果么?
      

  4.   

    select col from tb with(index=索引) where convert(varchar(10),col,120)='2011-09-02'
    有函数  用不了索引的
      

  5.   

    这不行,改成
    select col from tb
    where col>='2011-09-02'
    AND col<'2011-09-03'
      

  6.   

     col<convert(char(8),getdate(),120) and col>=convert(char(8),dateadd(dd,-1,getdate()),120) --今天的
    昨天的就是 -1天与-2天的关系。后面用函数应该不影响吧。
      

  7.   

    select * from Table where col between '2011-09-01' and '2011-09-01 23:59:59 999'
      

  8.   

    本帖最后由 roy_88 于 2011-09-02 16:07:54 编辑
      

  9.   

    这个时候就不能用date系列的函数处理了 ,建议 用 比较运算符 > < = 或者 用between吧~
      

  10.   

    当天或昨天,把函数放在getdate()而不要放在列上当天:
    where col>=convert(varchar(10),getdate(),120)
    AND col<convert(varchar(10),dateadd(day,1,getdate()),120)昨天:
    where col>=convert(varchar(10),dateadd(day,-1,getdate()),120)
    AND col<convert(varchar(10),getdate(),120)其他的可能查询方式我相信你自己可以同样写出。