我想在数据库的表中查询当前时间是否在表中的的开始时间和结束时间之间,不知查询语句怎么写,请指教。

解决方案 »

  1.   


    比如表中的“开始时间”和“结束时间”字段名为:startTimeendTime
    SELECT * FROM YourTable WHERE '2008.4.8 19:00:00' BETWEEN startTime AND endTime
    在 VC++ 中,只要生成上边的 SQL 语句即可。
      

  2.   

    SELECT * FROM YourTable WHERE getdate() BETWEEN startTime AND endTime
      

  3.   

    我的表里每条记录都有开始1楼所说的startTime和endTime,我现在是要查询当前时间在哪些记录的startTime和endTime之间,COleDateTime currentTime = COleDateTime::GetCurrentTime();
    strSQL.Format(_T("select the_time from c_timer where startTime <= '%s' and endTime >= '%s' "), currentTime );   这样写查询语句请帮看看。
      

  4.   

    上面写的有错,这样写:COleDateTime currentTime = COleDateTime::GetCurrentTime(); 
    strSQL.Format(_T("select the_time from c_timer where startTime  <= '%s' and endTime >= '%s' "), currentTime,currentTime  );   
      

  5.   

    strSQL.Format(_T("select the_time from c_timer where startTime   <= '%s' and endTime >= '%s' "), currentTime.Format("%y-%m-%d %H:%M:%S"),currentTime.Format("%y-%m-%d %H:%M:%S"));   
      

  6.   

    我试过了,照5楼这样根据字符串来查询是不准确的,只有根据datetime来查询,但不知道'%s' 用什么来表示datetime变量。
      

  7.   

    如果时间格式为年月日,则
    CTime cur=CTime::GetCurrent();
    CString strSql.Format("select * from table where startTime<='%s' and endTime>= '%s'",cur.Format("%Y-%m-%d"),cur.Format("%Y-%m-%d"))
    如果不是.就参照5楼的那样做.其实只是给你一个参照本.而不是一个答案.真正答案要靠自己实现的了
      

  8.   

    你用二楼的方法直接用数据库自己的getdate() 不就好了么?
      

  9.   

    strtemp1=m_timefrom.Format("%Y-%m-%d");
    strtemp2=m_timeto.Format("%Y-%m-%d");

    sql.Format("SELECT * FROM table WHERE table.日期 BETWEEN #%s# AND #%s#",strtemp1,strtemp2);
    我的这样 通过了的
      

  10.   

    m_timefrom m_timeto是COleDateTime类型