我有一个start_date和end_date。我想要得到这两个日期之间的日期列表。有人能帮我指出我的查询中的错误吗?
select Date,TotalAllowance
from Calculation
where EmployeeId=1
and Date between 2011/02/25 and 2011/02/27
这里Date是一个datetime变量。
我有一个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 to_date('2011/02/25','yyyy-mm-dd')
AND to_date ('2011/02/27','yyyy-mm-dd');
其他回答
select Date,TotalAllowance
from Calculation
where EmployeeId=1
and convert(varchar(10),Date,111) between '2011/02/25' and '2011/02/27'
select * from test
where CAST(AddTime as datetime) between '2013/4/4' and '2014/4/4'
——如果数据类型不同
这是非常古老的,但考虑到我有很多关于日期的经验,你可能会考虑这个:人们使用不同的区域设置,因此,一些人(以及一些数据库/计算机,取决于区域设置)可能会将11/12/2016这个日期读为11/dec 2016或11/12,2016。此外,提供给MySQL数据库的16/11/12将在内部转换为2016年11月12日,而运行在英国区域设置计算机上的Access数据库将解释并存储为2012年11月16日。
因此,每当我要与日期和数据库交互时,我都明确地制定了自己的策略。所以我总是提供我的查询和编程代码如下:
SELECT FirstName FROM Students WHERE DoB >= '11 Dec 2016';
还要注意Access将接受#,因此:
SELECT FirstName FROM Students WHERE DoB >= #11 Dec 2016#;
但是MS SQL服务器不会,所以我总是使用“'”如上所述,这两个数据库都接受。
当从代码中的变量中获得日期时,我总是将结果转换为字符串,如下所示:
"SELECT FirstName FROM Students WHERE DoB >= " & myDate.ToString("d MMM yyyy")
我写这篇文章是因为我知道有时一些程序员可能不够敏锐,无法检测到内在的转换。对于< 13的日期将不会出现错误,只是结果不同!
对于所问的问题,在最后一天的基础上加一天,做如下比较:
dated >= '11 Nov 2016' AND dated < '15 Nov 2016'
Select
*
from
Calculation
where
EmployeeId=1 and Date between #2011/02/25# and #2011/02/27#;
例子:工作和非工作。
select * from tblUser Where
convert(varchar(10),CreatedDate,111) between '2015/04/01' and '2016/04/01' //--**Working**
OR
select * from tblUser Where
(CAST(CreatedDate AS DATETIME) between CAST('2015/04/01' AS DATETIME) And CAST('2016/4/30'AS DATETIME)) //--**Working**
OR
select * from tblUser Where
(YEAR(CreatedDate) between YEAR('2015/04/01') And YEAR('2016/4/30'))
//--**Working**
下面的AND是无效的:
select * from tblUser Where
Convert(Varchar(10),CreatedDate,111) >= Convert(Varchar(10),'01-01-2015',111) and Convert(Varchar(10),CreatedDate,111) <= Convert(Varchar(10),'31-12-2015',111) //--**Not Working**
select * from tblUser Where
(Convert(Varchar(10),CreatedDate,111) between Convert(Varchar(10),'01-01-2015',111) And Convert(Varchar(10),'31-12-2015',111)) //--**Not Working**