Q:

Binary operator OR in TSQL?

What I 'm trying to achieve is to count occurrences in a sort of time line, considering overlapping events as a single one, starting from a field like this and using TSQL:
Pattern  (JSON array of couple of values indicating 
the start day and the duration of the event)
----------------------------------------------------
[[0,22],[0,24],[18,10],[30,3]]      
----------------------------------------------------

For this example the result expected should be 30
What i need is a TSQL function to obtain this number...
Even If I'm not sure it's the right path to follow, I'm trying to simulate a sort of BINARY OR between rows of my dataset.
After some trying I managed to turn my dataset into something like this:
start  | length | pattern
----------------------------------------------------
0      | 22     | 1111111111111111111111
0      | 24     | 111111111111111111111111
18     | 10     | 000000000000000001111111111
30     | 3      | 000000000000000000000000000000111
----------------------------------------------------

But now I dont' know how to proceed in TSQL =)
a solution as i said could be a binary OR between the "pattern" fields to obtain something like this:
1111111111111111111111...........
111111111111111111111111.........
000000000000000001111111111......
000000000000000000000000000000111
--------------------------------------
111111111111111111111111111000111

Is it possible to do it in TSQL?
Maybe i'm just complicating things here do you have other ideas?
DO NOT forget I just need the result number!!!
Thank you all

A:

Only the total days that an event occurs needs to be returned. 
But I was wondering how hard it would be to actually calculate that binary OR'd pattern.  
declare @T table (start int, length int);

insert into @T values 
(0,22),
(0,24),
(18,10),
(30,3);

WITH 
DIGITS as (
    select n 
    from (values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) D(n)
),
NUMBERS as (
  select (10*d2.n + d1.n) as n 
  from DIGITS d1, DIGITS d2 
  where (10*d2.n + d1.n) < (select max(start+length) from @T)
),
CALC as (
    select N.n, max(case when N.n between IIF(T.start>0,T.start,1) and IIF(T.start>0,T.start,1)+T.length-1 then 1 else 0 end) as ranged
    from @T T 
    cross apply NUMBERS N
    group by N.n
)
select SUM(c.ranged) as total,
stuff(
 (
    select ranged as 'text()'
    from CALC
    order by n
    for xml path('')
),1,1,'') as pattern
from CALC c;

Result:
total   pattern
30      11111111111111111111111111100111

A:

Depending on your input date, you should be able to do something like the following to calculate your Days With An Event.
The cte is used to generate a table of dates, the start and end of which are defined by the two date variables.  These would be best suited as data driven from your source data.  If you have to use numbered date values, you could simply return incrementing numbers instead of incrementing dates:
declare @Events table (StartDate    date
                        ,DaysLength int
                        )
insert into @Events values
 ('20160801',22)
,('20160801',24)
,('20160818',10)
,('20160830',3)

declare @StartDate date = getdate()-30
        ,@EndDate date = getdate()+30

;with Dates As  
(  
select DATEADD(day,1,@StartDate) as Dates
union all
select DATEADD(day,1, Dates)
from Dates
where Dates < @EndDate
)
select count(distinct d.Dates) as EventingDays
from Dates d
    inner join @Events e
        on(d.Dates between e.StartDate and dateadd(d,e.DaysLength-1,e.StartDate)
            )
option(maxrecursion 0)

