在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号。这个问题如何解决

解决方案 »

  1.   

    加条件:isdate(birthday)=1select  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
      

  2.   

    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