日期:2014-05-17  浏览次数:20445 次

SQL 求一个日期是否存在另外一张表并且是最近两天的工作日
本帖最后由 chenfeng_cstp 于 2013-03-28 20:36:14 编辑
比如今天是25号,则找到日期为21 22
如果今天是28号,则找到的是26 27号

另外一张表是

2013-03-23
2013-03-24

2013-03-30

最好是一条语句内写
SQL? 找最近两天

------解决方案--------------------
这个不好弄,在SQL中只能判断是不是星期六,日。 而节假日也有可能是在,1,2,3,4,5中,所以出来的结果不一定正确,因此这么做没有意义。
------解决方案--------------------
看看有没帮助
------解决方案--------------------
e.g.
use tempdb
go
if OBJECT_ID('#') is not null drop table #
create table #(col1 date) 
insert into #(col1) values('2013-03-20'),('2013-03-21'),('2013-03-22'),('2013-03-23'),('2013-03-24'),('2013-03-25'),('2013-03-26'),('2013-03-27'),('2013-03-28'),('2013-03-29')

---------------------------------------
declare @today date='2013-03-25'

select top(2) a.col1
   from # a 
   where DATEPART(WEEKDAY,a.col1) not in(1,7)
   and a.col1<@today
    order by a.col1 desc 

/*
col1
-----------------------
2013-03-22
2013-03-21
*/