我有一个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 * from test
where CAST(AddTime as datetime) between '2013/4/4' and '2014/4/4'
——如果数据类型不同
其他回答
例子:工作和非工作。
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**
这对我很有效
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 * from table_name where col_Date between '2011/02/25'
AND DATEADD(s,-1,DATEADD(d,1,'2011/02/27'))
这里,首先向当前的endDate添加一天,它将是2011-02-28 00:00:00,然后减去一秒,使结束日期为2011-02-27 23:59:59。通过这样做,您可以获得给定间隔之间的所有日期。
output:
2011/02/25
2011/02/26
2011/02/27
试着把日期放在# #之间 例如:
#2013/4/4# and #2013/4/20#
这对我很管用。
两件事:
使用引号 确保包括最后一天(结束于24) 选择日期,总津贴 从计算 EmployeeId = 1 and "2011/02/25" <= Date and Date <= "2011/02/27"
如果Date为DateTime。
我倾向于以这种方式进行范围检查,因为它清楚地显示了下限和上限。请记住,日期格式在不同的文化中有很大的不同。所以你可能要确保它被解释为一个日期。使用DATE_FORMAT(日期,'Y/m/d')。
(提示:使用STR_TO_DATE和DATE_FORMAT来切换范例。)