我有一个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 '2011/02/25' and '2011/02/27'
日期值需要作为字符串输入。
为了确保您的SQL Server 2008及更高版本的查询能够适应未来,Date应该被转义,因为在以后的版本中它是一个保留字。
请记住,没有时间的日期将午夜作为默认值,因此这里可能没有正确的值。
其他回答
试试这个:
select Date,TotalAllowance from Calculation where EmployeeId=1
and [Date] between '2011/02/25' and '2011/02/27'
日期值需要作为字符串输入。
为了确保您的SQL Server 2008及更高版本的查询能够适应未来,Date应该被转义,因为在以后的版本中它是一个保留字。
请记住,没有时间的日期将午夜作为默认值,因此这里可能没有正确的值。
如果日期在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 * from test
where CAST(AddTime as datetime) between '2013/4/4' and '2014/4/4'
——如果数据类型不同
实际上,为了得到最准确的结果,所有sql日期都应该是yyyy-MM-dd格式。
Select
*
from
Calculation
where
EmployeeId=1 and Date between #2011/02/25# and #2011/02/27#;