• You know I have done this myself but later came to wonder why people want to store all information for dual logic scenarios when they just need the smaller set. In the case of a production work date versus off dates typically the off dates are the smaller set. I would suggest just have a date table for non-production dates only which would hold the values for weekends and other non-production dates with the date column clustered indexed. These means storing around 110 dates a year for dates you don't work (could be saturdays, sundays, holidays or other days that are non-production in your environment) which is insignificant. So your query can be simply something like this

    select

    TOD.Ordernr

    ,TOD.Orderline

    ,datediff(dd, TOD.OrderDate, TOD.ShipDate) - count(NPD.DateColumn) as diff

    from

    dbo.TestOrderDetails TOD

    inner join

    dbo.NonProductionDates NPD

    ON

    NPD.DateColumn BETWEEN TOD.OrderDate AND TOD.ShipDate

    GROUP BY

    TOD.Ordernr

    ,TOD.Orderline

    ,TOD.OrderDate

    ,TOD.ShipDate

    Of course you need to manage logic of nulls and if day is inclusive (20 to 27 is 8 days) or exclusive (20 to 27 is 7 days). But it keeps logic very simple.