2013-06-18 82 views
0

我使用的是SQL Server的给定的月份和现在一年的指定日期范围内2008年我的表结构看起来像SQL Server查询,以检查是否

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 

当我提供特定monthno和年份它具有自动返回给我适当的行,月份和年份落在给定的日期范围内。我试着用下面的查询,但它不适用于明年的检查。任何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 

回答

0

对于这个执行它的工作单独的查询..

我尝试不使用where子句中不止一个条件语句..

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 
+0

如果在TODATE年大于@yearno参数,那么不需要检查月份。简单的逻辑..我希望这可以帮助你们..谢谢 –