表A,日期字段DATETMP,时间字段TIMETMP
请问如何用SQL查询出日期2007-1-1 10:00:00到2009-4-18 12:00:00中的所有记录?(注意日期时间是两个字段)
谢谢.

解决方案 »

  1.   


    select * from talbe where col between '2007-1-1 10:00:00' and '2009-4-18 12:00:00'
      

  2.   

    select * 
    from tb 
    where cast(DATETMP+' '+TIMETM as datetime) between '2007-1-1' and '2009-4-18'
      

  3.   

    select * 
    from tb 
    where cast(DATETMP+' '+TIMETM as datetime) between '2007-1-1 10:00:00' and '2009-4-18 12:00:00'改正.
      

  4.   

    两种写法:
    方法一:
    1.select * from tab where DATETIME between '2007-1-1 10:00:00' and '2009-4-18 12:00:00'方法二:
    2、select * from tab where DATETIME>'2007-1-1 10:00:00' and DATETIME<'2009-4-18 12:00:00'
      

  5.   

    大家理解错了, 我的意思是日期时间分别是两个字段
    我下面语句写得不对,就是那个意思.
    select * from tab where DATETMP>'2007-1-1' and TIMETMP< 12:00:00'
      

  6.   

    datetime类型本身就是包含年月日时分秒的,可以试一试把这两个字段转化为nvarchar型,然后再用substring提取两个日期中你想要的部分
      

  7.   

    select * from tb 
    where cast(DATETMP+' '+TIMETM as datetime) between '2007-1-1 10:00:00' and '2009-4-18 12:00:00'
      

  8.   


    楼主以下是不是你想要的结果
    select * from tab where DATETIME between '2007-1-1 10:00:00' and '2009-4-18 12:00:00'
    union 
    select * from tab where TIMETMP between '2007-1-1 10:00:00' and '2009-4-18 12:00:00'