表字段:日期count_date  时间count_time  数据data现在我想对2008-01-12 11:12:13 到 2008-03-12 15:11:58 之间的data字段求和。该怎样写sql语句呢?

解决方案 »

  1.   

    select sum(data) data
    from tb
    where convert(char(10),count_date,120) between '2008-01-12' and '2008-03-12'
             and convert(char(8),count_time,114) between '11:12:13' and '15:11:58'
      

  2.   

    随手敲的,为验证
    select sum(data) from table1 where count_date+' '+count_time between '2008-01-12 11:12:13' and '2008-03-12 15:11:58'
      

  3.   


    --假设日期count_date  时间count_time  为字符串型.
    select sum(data) from tb where cast(count_date + ' ' + count_time as datetime) between '2008-01-12 11:12:13' and '2008-03-12 15:11:58' 
      

  4.   

    select sum(data) from 表
    where count_date='2008-01-12' and
    (count_time between '11:12:13' and '15:11:58')
      

  5.   

    --假设日期count_date  时间count_time  为datetime型.
    select sum(data) from tb where cast(convert(varchar(10),count_date,120) + ' ' + convert(varchar(8),count_time,114) as datetime) between '2008-01-12 11:12:13' and '2008-03-12 15:11:58' 
      

  6.   

    select sum(data) as data  from tb
    where dt+' '+tm between '2008-03-25 10:54:24.670' and '2008-03-25 22:54:24.670'直接连就行