当前位置: 代码迷 >> Sql Server >> SQLSERVER长年和闰年时间差
  详细解决方案

SQLSERVER长年和闰年时间差

热度:52   发布时间:2016-04-24 21:11:51.0
SQLSERVER平年和闰年时间差
在SQLSERVER中我要获取两个时间的间隔我用select  top 10 name,bigclassid , birthday   from   [user] where  leave='NO'  and  replace(birthday,year(birthday),year(getdate()))-getdate() between   -1   and  6  order by month(birthday),DAY(birthday) asc但是提示报错“从 varchar 数据类型到 datetime 数据类型的转换产生一个超出范围的值。”错误原因是数据库中有条数据是‘1992-02-29’这是闰年,但今年是平年2月没有29号。这个问题如何解决

------解决方案--------------------
加条件:isdate(birthday)=1

select  top 10 name,bigclassid , birthday   
from   [user] 
where  leave='NO'  and  replace(birthday,year(birthday),year(getdate()))-getdate() between   -1   and  6  and isdate(birthday)=1
order by month(birthday),DAY(birthday) asc
------解决方案--------------------
select  top 10 name,bigclassid , birthday   
from   [user] 
where  leave='NO'  and  
datediff(day,dateadd(yy,DATEDIFF(yy, birthday, GETDATE()),birthday),dateadd(day,6,getdate())) between 0 and 6
order by month(birthday),DAY(birthday) asc
  相关解决方案