--解决闰年2月29日转换报错问题
if(datepart(mm,getdate())='2' and datepart(dd,getdate())='29')
begin
select * from client c
where datediff(dd, cast(rtrim(datepart(yy,c.ContactBrithday))+'-'+
rtrim(datepart(mm,getdate()))+'-'+
rtrim(datepart(dd,getdate())-1) as datetime),c.ContactBrithday) between 0 And 5
end
else
begin
select * from client c
where datediff(dd, cast(rtrim(datepart(yy,c.ContactBrithday))+'-'+
rtrim(datepart(mm,getdate()))+'-'+
rtrim(datepart(dd,getdate())) as datetime),c.ContactBrithday) between 0 And 5
or
--解决跨年问题
datediff(dd, cast(rtrim(datepart(yy,c.ContactBrithday)-1)+'-'+
rtrim(datepart(mm,getdate()))+'-'+
rtrim(datepart(dd,getdate())) as datetime),c.ContactBrithday) between 0 And 5
end
if(datepart(mm,getdate())='2' and datepart(dd,getdate())='29')
begin
select * from client c
where datediff(dd, cast(rtrim(datepart(yy,c.ContactBrithday))+'-'+
rtrim(datepart(mm,getdate()))+'-'+
rtrim(datepart(dd,getdate())-1) as datetime),c.ContactBrithday) between 0 And 5
end
else
begin
select * from client c
where datediff(dd, cast(rtrim(datepart(yy,c.ContactBrithday))+'-'+
rtrim(datepart(mm,getdate()))+'-'+
rtrim(datepart(dd,getdate())) as datetime),c.ContactBrithday) between 0 And 5
or
--解决跨年问题
datediff(dd, cast(rtrim(datepart(yy,c.ContactBrithday)-1)+'-'+
rtrim(datepart(mm,getdate()))+'-'+
rtrim(datepart(dd,getdate())) as datetime),c.ContactBrithday) between 0 And 5
end
思路是把当前日期的年份用生日年份替换,这样在转换日期1981-2-29如果报错的话只要通过判断
当前日期是否刚好2月29日就成了