sql - DateDiff use current date if enddate is null -
i calculating 2 dates , returning days want exclude weekends , holidays. want say, if enddate null, use current date?
how do datediff?
or use this..
if object_id('tempdb..#holidays') not null drop table temp..#holidays create table #holidays(id int identity(1,1),holiday date) insert #holidays values ('2015-12-25'),('2015-12-28') set dateformat ymd declare @startdate date = '2015-12-01' declare @endtdate date = '2015-12-31' --some times @endtdate might null so, set getdate() set @endtdate = isnull(@endtdate,cast(getdate() date)) declare @holiday int set @holiday = (select count(*) #holidays holiday between @startdate , @endtdate) select (datediff(dd, @startdate, @endtdate) + 1) -(datediff(wk, @startdate, @endtdate) * 2) -(case when datename(dw, @startdate) = 'sunday' 1 else 0 end) -(case when datename(dw, @endtdate) = 'saturday' 1 else 0 end) -@holiday
Comments
Post a Comment