0

我正在使用 SQL Server 2008。我的表结构看起来像

SNo  From_date   To_date     EmpId
----------------------------------
1    6/6/2012   2/6/2013     1
2    3/6/2013   NULL         1
3    6/6/2012   5/12/2012    2

当我提供特定的月份和年份时,它必须自动返回给定日期范围内月份和年份的适当行。我尝试使用以下查询,但它不适用于明年的检查。任何 1 都可以帮助我解决这个问题吗?

  declare @monthno int;
  declare @yearno int;
  set @monthno=7;
  set @yearno=2012;
  select * from YearMonthTable
  where (@monthno>= MONTH(From_Date) and @yearno >= YEAR(From_Date)) 
  and ((@monthno<= MONTH(To_date) and @yearno <=YEAR(To_date)) or To_date is null)
  and EmpId=1  
4

2 回答 2

0

为此执行单独的查询,它正在工作..

我的尝试是在 where 子句中不使用多个条件语句..

于 2013-06-18T08:48:20.527 回答
0
  declare @monthno int;
  declare @yearno int;
  set @monthno=7;
  set @yearno=2012;
  select * from YearMonthTable
  where (@monthno>= MONTH(From_Date) and @yearno >= YEAR(From_Date)) 
  and ( (@yearno < YEAR(To_date)) or (@monthno <= MONTH(To_date) and  earno =YEAR(To_date))  or To_date is null)  and EmpId=1  
于 2016-03-24T14:06:12.530 回答