字段:进出时间
类型:datatime
格式:2000-01-01 00:00:00
SLQ:"select dtb where 进出时间 between '" +DateTime.Now.Date+ " 00:00:00' and '" +DateTime.Now.Date+ "23:59:59'";
为什么取不出,当天的记录,说格式不对,请问应该怎么改?

解决方案 »

  1.   

    "select dtb from 表 where 进出时间 between '" +DateTime.Now.Date+ " 00:00:00' and '" +DateTime.Now.Date+ "23:59:59'";
    缺少from表 
      

  2.   

    你把SLQ打印出来不就知道哪里错误了...在SQL Server里面,获得当前时间是 Getdate() 函数,带时间的。
      

  3.   

    我试了.是从 "char 数据类型到 datetime 数据类型的转换导致 datetime 值越界"。
    请问,格式应该怎么转?
      

  4.   

    select dtb from 表 where 进出时间 >=convert(datetime,convert(varchar(8),getdate(),112)) 
    and 进出时间 < dateadd(day,1,convert(datetime,convert(varchar(8),getdate(),112)))
      

  5.   

    根据提示使用cast或convert先进行日期时间转换
      

  6.   

    convert(varchar(10),@YourDateTime,112)
      

  7.   

    试试:
    select dtb where 进出时间 between cast('" +DateTime.Now.Date+ " 00:00:00' as datetime) and cast('" +DateTime.Now.Date+ "23:59:59' as datetime)
      

  8.   

    convert(varchar(XX),DateTime字段,121),年月日为10后加一个空格,时:分:秒等,看取值精度决定你取多长的数据。
      

  9.   

    把DateTime.Now里的年月日分别取出来重新组合,我是这样做的。
      

  10.   

    "SELECT * FROM Table WHERE [进出时间] between 
    '" +DateTime.Now.ToShortDateString()+ "' and '" + DateTime.Now.AddDays(1).ToShortDateString() +'";OR"SELECT * FROM Table WHERE [进出时间]>=
    '" +DateTime.Now.ToShortDateString()+ "' and [进出时间]<'" + DateTime.Now.AddDays(1).ToShortDateString() +'";
      

  11.   

    时间段的话
    "SELECT * FROM Table WHERE [进出时间] between 
    '" +DateTime.Now.ToShortDateString()+ " 02:30:00' and '" + DateTime.Now.AddDays(1).ToShortDateString() +" 03:30:00 '"; 
      

  12.   

    select dtb where 进出时间 between cast('" +DateTime.Now.Date+ " 00:00:00' as datetime) and cast('" +DateTime.Now.Date+ "23:59:59' as datetime)
      

  13.   


    CTime tm1, tm2;          str.Format(_T("select dtb where [进出时间] >= #%s# and [进出时间] <= #%s %s#"), 
                    tm1.Format("%Y-%m-%d"),
                    tm2.Format("%Y-%m-%d"),      
                    _T("23:59:59"));
      

  14.   


    select dtb from 表 where 进出时间>CONVERT(char(10),GETDATE(),120) and 进出时间<CONVERT(char(10),DATEADD(day,1,GETDATE()),120)试试!