判断 节假日 终极解决方案(带五一 十一,端午节,春节,清明节)

if exists (select * from dbo.sysobjects where id = object_id(N'[tb_Holiday]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)  
drop table [tb_Holiday]  
GO  
create table tb_Holiday(id int IDENTITY(1,1) NOT NULL primary key clustered,HolidayDate smalldatetime,HolidayName nvarchar(50) not null,Holidayvalue bigint not null,Type char(1) not null)
Go
Insert into tb_Holiday(HolidayDate,HolidayName,Holidayvalue,Type)
          select '2009-01-01','元旦',1,'1'
union all select '2009-01-02','元旦',1,'1'
union all select '2009-01-03','元旦',1,'1'
union all select '2009-01-04','公休(周末)',128,'0'
union all select '2009-01-25','春节',2,'1'
union all select '2009-01-26','春节',2,'1'
union all select '2009-01-27','春节',2,'1'
union all select '2009-01-28','春节',2,'1'
union all select '2009-01-29','春节',2,'1'
union all select '2009-01-30','春节',2,'1'
union all select '2009-01-31','春节',2,'1'
union all select '2009-01-24','公休(周末)',128,'0'
union all select '2009-02-01','春节',2,'0'
union all select '2009-04-04','清明节',4,'1'
union all select '2009-04-05','清明节',4,'1'
union all select '2009-04-06','清明节',4,'1'
union all select '2009-05-01','劳动节',8,'1'
union all select '2009-05-02','劳动节',8,'1'
union all select '2009-05-03','劳动节',8,'1'
union all select '2009-05-28','端午节',16,'1'
union all select '2009-05-29','端午节',16,'1'
union all select '2009-05-30','端午节',16,'1'
union all select '2009-05-31','公休(周末)',128,'0'
union all select '2009-10-01','国庆节、中秋节',32,'1'
union all select '2009-10-02','国庆节、中秋节',32,'1'
union all select '2009-10-03','国庆节、中秋节',32,'1'
union all select '2009-10-04','国庆节、中秋节',32,'1'
union all select '2009-10-05','国庆节、中秋节',32,'1'
union all select '2009-10-06','国庆节、中秋节',32,'1'
union all select '2009-10-07','国庆节、中秋节',32,'1'
union all select '2009-10-08','国庆节、中秋节',32,'1'
union all select '2009-09-27','公休(周末)',128,'0'
union all select '2009-10-10','公休(周末)',128,'0'
union all select '2010-01-01','元旦',1,'1'
union all select '2010-01-02','元旦',1,'1'
union all select '2010-01-03','元旦',1,'1'

select distinct(HolidayName) from tb_Holiday where HolidayDate >=convert(datetime,'2009-2-2') and HolidayDate <= convert(datetime,'2009-5-30') or Holidayvalue =128

CREATE  FUNCTION   [dbo].[fnCheckDate](@solarDay DATETIME)        
  RETURNS   bigint   AS          
  BEGIN          
      DECLARE   @OUTPUTDATA   Bigint   --返回数值
      SET @OUTPUTDATA = 0   --初始化为非假日
      select @OUTPUTDATA=@OUTPUTDATA|Holidayvalue from tb_Holiday where Holidayvalue<>128 and HolidayDate=@solarDay
      IF (@OUTPUTDATA = 0)
         Begin
   IF (@@DATEFIRST+DATEPART(Weekday,@solarDay)-1)%7 in(0,6) And not Exists(select * from tb_Holiday where HolidayDate=@solarDay and Type='0') 
    select @OUTPUTDATA = 128
         End
      Return @OUTPUTDATA
  END  

原文地址:https://www.cnblogs.com/xianzuoqiaoqi/p/1510584.html