来自sql server 2005的超出范围的答案

时间:2013-03-11 08:34:52

标签: sql sql-server

select 
  e.E_ID as Employee_ID,
  e.E_NAME as Employee_Name, 
  convert(varchar(10),a.Date,103) as Date ,
  convert(varchar(8),a.Timein,108) as Time_In, 
  convert(varchar(8),a.Timeout,108) as Time_Out 
from Employee e 
LEFT JOIN Attendance a  
  ON a.E_ID = e.E_ID 
  AND (convert(varchar(10),a.Date,103) between '01/02/2013' And '28/02/2013')

上面是我的查询,我从两个表中获取数据,但是它也打印了1月份的值,而不是查询。

我已经调试了一个小时,请任何人帮忙。

2 个答案:

答案 0 :(得分:2)

BETWEENdd/mm/yyyy格式化的字符串无法正常运行!

要在二月份使用

返回行
   LEFT JOIN Attendance a
     ON a.E_ID = e.E_ID  AND a.Date BETWEEN '20130201' AND '20130228'

或者a.Date中的值可能包含00:00:00

以外的时间成分
   LEFT JOIN Attendance a
     ON a.E_ID = e.E_ID  AND a.Date >= '20130201' AND a.Date < '20130301'

将列投射到varchar是不可分割的,这意味着无法使用索引查找,

答案 1 :(得分:1)

Try below and see if it helps

select 
  e.E_ID as Employee_ID,
  e.E_NAME as Employee_Name, 
  convert(varchar(10),a.Date,103) as Date ,
  convert(varchar(8),a.Timein,108) as Time_In, 
  convert(varchar(8),a.Timeout,108) as Time_Out 
  from Employee e 
  LEFT JOIN Attendance a  
  ON a.E_ID = e.E_ID 
  Where (convert(varchar(10),a.Date,103) between '01/02/2013' And '28/02/2013')