我有一个start_date和end_date。我想要得到这两个日期之间的日期列表。有人能帮我指出我的查询中的错误吗?

select Date,TotalAllowance 
from Calculation 
where EmployeeId=1
  and Date between 2011/02/25 and 2011/02/27

这里Date是一个datetime变量。


当前回答

你应该把这两个日期放在单引号之间,比如..

select Date, TotalAllowance from Calculation where EmployeeId = 1
             and Date between '2011/02/25' and '2011/02/27'

或者可以使用

select Date, TotalAllowance from Calculation where EmployeeId = 1
             and Date >= '2011/02/25' and Date <= '2011/02/27'

请记住,第一次约会包含所有内容,但第二次约会就不包含所有内容,因为它实际上是“2011/02/27 00:00:00”。

其他回答

如果日期在24小时内,从早上开始,到晚上结束,应该添加如下内容:

declare @Approval_date datetime
set @Approval_date =getdate()
Approval_date between @Approval_date +' 00:00:00.000' and @Approval_date +' 23:59:59.999'
select Date,TotalAllowance 
from Calculation 
where EmployeeId=1
  and convert(varchar(10),Date,111) between '2011/02/25' and '2011/02/27'

这对我很有效

SELECT 
  * 
FROM 
  `request_logs` 
WHERE 
  created_at >= "2022-11-30 00:00:00" 
  AND created_at <= "2022-11-30 20:04:50" 
ORDER BY 
  `request_logs`.`id` DESC

SELECT CITY, COUNT(EID) OCCURENCES FROM EMP 
WHERE DOB BETWEEN '31-JAN-1900' AND '31-JAN-2900' 
GROUP BY CITY 
HAVING COUNT(EID) > 2;

该查询将查找DOB位于员工指定时间范围内的次数超过2次的城市。

我们可以使用between来显示两个日期数据,但这将搜索整个数据并进行比较,因此对于巨大的数据,它会使我们的过程变慢,所以我建议每个人都使用datediff:

qry = "SELECT * FROM [calender] WHERE datediff(day,'" & dt & "',[date])>=0 and datediff(day,'" & dt2 & "',[date])<=0 "

这里的日历是表,dt作为开始日期变量,dt2是结束日期变量。