已知表  work
id                       topdate                          name
1               2007/12/29 上午 10:26:35                     q
2               2007/12/29 上午 10:32:47                     a
3               2007/12/29 上午 10:32:48                     c
4               2008/1/2 上午 10:21:31                       s
5               2008/1/2 上午 10:24:36                       d
6               2008/1/2 上午 10:25:31                       f
求在某个时间段内的值(其中这个时间段要包含  年月日时分秒)
其中topdate 类型为datetime

解决方案 »

  1.   

    select * from work where topdate between 'startdate' and 'enddate'
    --
    select * from work where topdate>='startdate' and 'startdate'<='enddate'
      

  2.   


    declare @stardate datetime
    declare @enddate datetime
    set @stardate=''
    set @enddate=''
    select * from work where topdate between @stardate and @enddate
      

  3.   

    select   *   from   work   where   topdate> ='startdate'   and   topdate <='enddate' --
    ---
      

  4.   

    select * from work where convert(varchar(19) , topdate , 120) >= '2008-01-03 00:00:00' and convert(varchar(19) , topdate , 120) <= '2008-01-03 23:59:59'
      

  5.   

    create table tb(id int , topdate datetime , name varchar(10))
    insert into tb values(1, '2007/12/29 10:26:35', 'q') 
    insert into tb values(2, '2007/12/29 10:32:47', 'a') 
    insert into tb values(3, '2007/12/29 10:32:48', 'c') 
    insert into tb values(4, '2008/1/2 10:21:31', 's') 
    insert into tb values(5, '2008/1/2 10:24:36', 'd') 
    insert into tb values(6, '2008/1/2 10:25:31', 'f') 
    goselect * from tb where convert(varchar(19) , topdate , 120) >= '2008-01-02 00:00:00' and convert(varchar(19) , topdate , 120) <= '2008-01-02 23:59:59'
    /*
    id          topdate                 name
    ----------- ----------------------- ----------
    4           2008-01-02 10:21:31.000 s
    5           2008-01-02 10:24:36.000 d
    6           2008-01-02 10:25:31.000 f(3 行受影响)
    */select * from tb where convert(varchar(19) , topdate , 120) >= '2008-01-02 00:00:00' and convert(varchar(19) , topdate , 120) <= '2008-01-02 10:24:36'
    /*
    id          topdate                 name
    ----------- ----------------------- ----------
    4           2008-01-02 10:21:31.000 s
    5           2008-01-02 10:24:36.000 d(2 行受影响)
    */drop table tb