• Something like this could do the trick and you can convert it into an inline table valued function which would be as fast as the normal query.

    DECLARE @Start date = '20120918',

    @End date = '20140918';

    WITH E1(N) AS (

    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL

    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL

    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1

    ), --10E+1 or 10 rows

    E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows

    E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max

    cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front

    -- for both a performance gain and prevention of accidental "overruns"

    SELECT TOP (DATEDIFF(DD, @Start, @End) + 1)

    DATEADD( DD, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) - 1, @Start)

    FROM E4

    )

    SELECT N

    FROM cteTally l

    WHERE DATENAME(DW, N) NOT IN( 'Saturday', 'Sunday');

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2