我有一表记录如下: 表名 A表
   C日期         D日期
   2009-02-03    2009-02-04
   2009-02-04    2009-02-02我要写一语句,譬如查询 2009-02-04 这天有哪几条记录, 要求是 以两个日期中大者参加判断.如查询 2009-02-04  结果是两条记录都 显示,因为两个最大日期都为 2009-02-04  ,如查询2009-02-03 则不返回任何记录.
decare @rq datetime
set @rq='2009-02-04'
select * from a表 不知后面条件该如何写.谢谢

解决方案 »

  1.   

    select *
    from tb
    where
    datediff(dd,case when c>d then c else d end,'2009-02-03')=0
      

  2.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([C] datetime,[D] datetime)
    insert [tb]
    select '2009-02-03','2009-02-04' union all
    select '2009-02-04','2009-02-02'
     
    ---查询---
    select *
    from tb
    where
    datediff(dd,case when c>d then c else d end,'2009-02-03')=0
    /**
    C                                                      D                                                      
    ------------------------------------------------------ ------------------------------------------------------ (所影响的行数为 0 行)
    **/select *
    from tb
    where
    datediff(dd,case when c>d then c else d end,'2009-02-04')=0
    /**
    C                                                      D                                                      
    ------------------------------------------------------ ------------------------------------------------------ 
    2009-02-03 00:00:00.000                                2009-02-04 00:00:00.000
    2009-02-04 00:00:00.000                                2009-02-02 00:00:00.000(所影响的行数为 2 行)
    **/
      

  3.   

    select *
    from tb
    where
    datediff(dd,C,'2009-02-03')=0 OR datediff(dd,D,'2009-02-03')=0