[dba-SQLServer] Series of Sundays in a query

Steve Erbach erbachs at gmail.com
Wed Dec 10 09:37:21 CST 2008


David,

Righto.  Makes perfect sense.

Steve Erbach
Neenah, WI

On Tue, Dec 9, 2008 at 7:59 PM, David McAfee <davidmcafee at gmail.com> wrote:
> You're welcome.
>
> I decided on having the number of weeks as a parameter that way if you
> need to call it for a different length you can reuse the same udf.
>
> On Tue, Dec 9, 2008 at 5:03 PM, Steve Erbach <erbachs at gmail.com> wrote:
>> David,
>>
>> Excellent!  I'd thought of possibly making a UDF; just never followed
>> through, but that makes perfect sense.  Thank you.
>>
>> Steve Erbach
>>
>> On Tue, Dec 9, 2008 at 5:56 PM, David McAfee <davidmcafee at gmail.com> wrote:
>>> I don't know if is any better, but you can create a udf such as:
>>>
>>> CREATE FUNCTION udfWeekBegins (@StartDate AS DATETIME, @NumOfWks AS
>>> INT) RETURNS @ListTable TABLE (WeekBeginning DATETIME) AS
>>> BEGIN
>>>    DECLARE @LoopDate AS DATETIME SET @LoopDate = @StartDate
>>>    WHILE @LoopDate < DATEADD(wk, at NumOfWks, at StartDate)
>>>        BEGIN
>>>            INSERT INTO @ListTable(WeekBeginning) SELECT @LoopDate
>>>            SET @LoopDate = DATEADD(dd,7, at LoopDate)
>>>        END
>>>    RETURN
>>> END



More information about the dba-SQLServer mailing list