我正在使用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
当我提供特定的monthno和year时,它必须自动返回适当的行,其中月份和年份在给定的日期范围内。我尝试使用以下查询,但它不适用于明年检查。任何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 :(得分:0)
为此执行单独的查询,它正在工作..
我的尝试不是在where子句中使用多个条件语句。
答案 1 :(得分: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