2013-06-18 123 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參數,那麼不需要檢查月份。簡單的邏輯..我希望這可以幫助你們..謝謝 –