select * from 表 where birthday_Month=5 and Birthday_Day between 26 and 28
select * from t1 where cast('1900-'+cast(Birthday_Month as varchar(2))+'-'+cast(Birthday_Day as varchar(2)) as datetime) between '1900-5-26' and '1900-6-3'
这个不能转换成日期来比较,只能从两个数字来比较。 这里的生日可能存在的是农历的生日,会出错的 比如select cast('2011-2-29' as datetime) 2011年农历2月29日这天,公历是2011年4月2号。但是农历2月29转换成日期格式就报错了。
--不跨年 即@开始月*100+开始日<=@结束月*100+@结束日 select * from tb where Birthday_Month*100+Birthday_Day between @开始月*100+开始日 and @结束月*100+@结束日--跨年 即@开始月*100+开始日>@结束月*100+@结束日 select * from tb where Birthday_Month*100+Birthday_Day >= @开始月*100+开始日 and Birthday_Month*100+Birthday_Day<=@结束月*100+@结束日
--不跨年(@开始月<=@结束月) 即@开始月*100+开始日<=@结束月*100+@结束日 if @开始月<=@结束月 then select * from tb where Birthday_Month*100+Birthday_Day between @开始月*100+开始日 and @结束月*100+@结束日 else --跨年:分成不跨年的部分和跨年的部分, --不跨年的部分只需大于开始日期,跨年的部分只需小于结束日期 select * from tb where Birthday_Month*100+Birthday_Day >= @开始月*100+开始日 or Birthday_Month*100+Birthday_Day<=@结束月*1300+@结束日
where cast('1900-'+cast(Birthday_Month as varchar(2))+'-'+cast(Birthday_Day as varchar(2)) as datetime)
between '1900-5-26' and '1900-6-3'
这里的生日可能存在的是农历的生日,会出错的
比如select cast('2011-2-29' as datetime)
2011年农历2月29日这天,公历是2011年4月2号。但是农历2月29转换成日期格式就报错了。
select * from tb where Birthday_Month*100+Birthday_Day between @开始月*100+开始日 and @结束月*100+@结束日--跨年 即@开始月*100+开始日>@结束月*100+@结束日
select * from tb where Birthday_Month*100+Birthday_Day >= @开始月*100+开始日 and Birthday_Month*100+Birthday_Day<=@结束月*100+@结束日
--不跨年(@开始月<=@结束月) 即@开始月*100+开始日<=@结束月*100+@结束日
if @开始月<=@结束月 then
select * from tb
where Birthday_Month*100+Birthday_Day
between @开始月*100+开始日 and @结束月*100+@结束日
else
--跨年:分成不跨年的部分和跨年的部分,
--不跨年的部分只需大于开始日期,跨年的部分只需小于结束日期
select * from tb
where Birthday_Month*100+Birthday_Day >= @开始月*100+开始日
or Birthday_Month*100+Birthday_Day<=@结束月*1300+@结束日
多谢!