当前位置: 代码迷 >> Sql Server >> SQL 中在一段日期内找出不存在的日期中最大的 50分
  详细解决方案

SQL 中在一段日期内找出不存在的日期中最大的 50分

热度:213   发布时间:2016-04-27 10:54:09.0
SQL 中在一段日期内找到不存在的日期中最大的 50分
比如 SQL这段日期 2012-09-01 至 2012-09-13
已有的日期
2012-09-01
2012-09-02
2012-09-10
2012-09-13
需要找到
2012-09-12 
 

------解决方案--------------------
SQL code
    create table tb (date_time datetime)    insert tb select '2012-09-01' union select '2012-09-02' union select '2012-09-10' union select '2012-09-13'         go    select * from tb    /*    date_time2012-09-01 00:00:00.0002012-09-02 00:00:00.0002012-09-10 00:00:00.0002012-09-13 00:00:00.000    */declare @sdate datetime declare @edate datetime set @sdate = '2012-09-01' set @edate = '2012-09-13' select MAX (dd)dd from (select     convert(varchar(10),dateadd(dd,num,@sdate),120) ddfrom     (select isnull((select count(1) from sysobjects where id <t.id),0) as num from sysobjects t) a where     dateadd(dd,num,@sdate) <[email protected] )a     where dd not in (select date_time from tb )  /*  dd2012-09-12*/
  相关解决方案