SQL查询选择日期范围之间的记录 [英] Sql query for selecting records in between Date range

查看:860
本文介绍了SQL查询选择日期范围之间的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据以下条件编写查询以选择记录:

I am trying to write a query for selecting records based on:


  1. 它们应属于特定类别

  2. 它们应该在开始日期和结束日期之间。

  1. They should belong to particular category
  2. They should be between Start date and End date.

我有以下记录:

EventId| EventName| Event_Category| Event_StartDate| Event_EndDate|
   1   |  aaa     |     4         |  2014-06-10    |  2014-06-15  |
   2   |  bbb     |     5         |  2014-06-10    |  2014-06-15  |
   3   |  ccc     |     6         |  2014-06-10    |  2014-07-11  |
   4   |  ddd     |     5         |  2014-06-01    |  2014-07-10  |
   5   |  eee     |     5         |  2014-06-10    |  2014-06-014 |

存储过程:

create proc usp_filterevnts @Categoryid int, @StartDate date, @EndDate date
as
begin 
select * from Events Where Event_Category=@Categoryid  and ( Event_Startdate>@StartDate and Event_Enddate<@EndDate )
end

如果我将sp执行为

  exec usp_filterevnts 5,'2014-06-09','2014-06-16' 

它应该显示第5类记录的记录,其开始日期和结束日期在'2014-06-09'和'2014-06-16之间。第二条记录,第五条记录。

It should display records of Category 5Th records , whose start date and end date between '2014-06-09' and '2014-06-16 ..ie 2nd record, 5 th record..

我的存储过程是否对此正确?

Is my stored procedure correct for this??


推荐答案

select * from Events Where Event_Category=@Categoryid and (Event_Startdate between @StartDate and @EndDate) and (Event_Enddate between @StartDate and @EndDate)

这篇关于SQL查询选择日期范围之间的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆