declare @stDate varchar(8),@enDate varchar(8)
select @stDate='2005/08',@enDate='2006/01'select *
from yourtable
where [year] between convert(int,left(@stDate,4)) and convert(int,left(@enDate,4))
and [month] beteen convert(int,right(@stDate,2)) and convert(int,right(@enDate,2))
select @stDate='2005/08',@enDate='2006/01'select *
from yourtable
where [year] between convert(int,left(@stDate,4)) and convert(int,left(@enDate,4))
and [month] beteen convert(int,right(@stDate,2)) and convert(int,right(@enDate,2))
union
select * from tb where year=2006 and month<=1
---or
select * from tb where rtrim(year)+'/'+rtrim(right('0'+rtrim(month),2) between '2005/08' and '2006/01'
我希望是一条SQL语句,不想用存储过程,谢谢。to zxkid(没有人会像我这样...):
你这种方法是最最简单的方法,非常感谢。
另外,顺便问一句,你以前是不是也遇到这种问题呀?否则,怎么能一下子就想出这种方法呢?高手呀。to wangdehao(找找找):
谢谢你,你的方法也不错,跟楼上的类似,但不如楼上的最直接。另外,还有个小问题:
2005、8、2006、1,这四个数字是外面传进来的,所以在 between 200508 and 200601时,我怎么拼起来比较好一点呢?
如果用SQL语句来拼,大概是怎么拼呢?
传进的值即可啊?
不过有点错,改下declare @stDate varchar(8),@enDate varchar(8)
select @stDate='2005/08',@enDate='2006/01'select *
from yourtable
where ([year] = convert(int,left(@stDate,4)) and [month]>=convert(int,right(@stDate,2))) or ([year] = convert(int,left(@enDate,4)) and [month]<=convert(int,right(@enDate,2)))