表结构如下
create table1(fid int not null,fyear int not null,fmonth int not null...)
其中fyear为年度fmonth为月份,现想查询2006年9月份至2006年11月份之间的数据查询的条件如何写,谢谢(我想到将年度月份转换成日期再比较,不知道还有没有别的方法)
...全文
22114打赏收藏
两个数值字段比较的查询如何写
表结构如下 create table1(fid int not null,fyear int not null,fmonth int not null...) 其中fyear为年度fmonth为月份,现想查询2006年9月份至2006年11月份之间的数据查询的条件如何写,谢谢(我想到将年度月份转换成日期再比较,不知道还有没有别的方法)
现想查询2006年9月份至2007年3月份之间的数据查询的条件如何写(年月是变化的)
-----------------------------------------------------------------------------------------
select
*
from
table1
where
(fyear>@bYear and fyear<eYear)
or
(fyear=@bYear and fmonth>=@bmonth)
or
(fyear=@eYear and fmonth<=@emonth)
==>
select
*
from
table1
where
(fyear>2006 and fyear<2007)
or
(fyear=2006 and fmonth>=9)
or
(fyear=2007 and fmonth<=3)
select * from table1 where (cast(fyear as varchar(4)) + '-' + rtrim(cast(fmonth as varchar(2))) + '01') >= '2006-09-01' and (cast(fyear as varchar(4)) + '-' + rtrim(cast(fmonth as varchar(2))) + '01') <= '2006-11-30'