如何使用SQL查询检查日期范围是否在给定日期
问题描述:
im创建一条记录,其开始日期为1-1-2013,结束日期为3-2-2013 ,我有另一个保存的记录,开始日期为''21-1-2013'',结束日期为''31-1-2013''我想比较两个记录日期与SQL查询如何做到这一点? br $>
谢谢
i m creating a record with start date as ''1-1-2013'' and end date as ''3-2-2013'', and i have another saved record with start date as ''21-1-2013'' and end date as ''31-1-2013'' i want to compare both records dates with sql queries how to do it ?.
Thanks
答
Hello nrkhi401,
我没有得到你的查询,但我理解的是:
你想写的查询只有在给定时才会返回结果,并且结束日期介于''1-1-之间2013''和''3-2-2013''
以下为此解决方案假设考虑
Hello nrkhi401 ,
I didn''t got your query but what I understood is like :
"You want to write query which will return result only when given from and end date falls between ''1-1-2013'' and ''3-2-2013''"
and for this below is the solution suppose consider
CREATE PROCEDURE(@FrmDate DATE , @EndDate DATE)
BEGIN
SELECT * FROM tbl WHERE
tblStDate BETWEEN @FrmDate AND @EndDate
AND
tblEdDate BETWEEN @FrmDate AND @EndDate
AND
@FrmDate BETWEEN tblStDate AND tblEdDate
AND
@EndDate BETWEEN tblStDate AND tblEdDate
END
希望这有帮助
--RDBurmon
Hope this helps
--RDBurmon
0)使用ISO 8601格式(YYYY-MM-DD)
1)不要存储为文本,存储为DATE或DATETIME
2)看看BETWEEN
3)你是什么试图找出这两个记录?
0) Use an ISO 8601 format (YYYY-MM-DD)
1) Don''t store as text, store as DATE or DATETIME
2) Look into BETWEEN
3) What are you trying to find out about these two records?