Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6641681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:50:22+00:00 2026-05-25T23:50:22+00:00

I have written the below VIEW using a UDF, but am wondering if there

  • 0

I have written the below VIEW using a UDF, but am wondering if there is a way to do this without needing a UDF. The only other option I could find is to SELECT UNION hardcoded values, but I don’t like that. Any suggestions or should I just leave it as it is? I am using MS SQL Server 2008 R2, so anything available there is free game.

CREATE FUNCTION GetN(@N int)
RETURNS @numbers TABLE (N int PRIMARY KEY NOT NULL)
AS
BEGIN
    DECLARE @i int
    SET @i = 0
    WHILE (@i < @N)
    BEGIN
        INSERT @numbers SELECT @i
        SET @i = @i + 1
    END
    RETURN
END
GO

CREATE VIEW Last10Weeks AS
SELECT DATEPART(wk, GETDATE())-N as WeekNumber
, DATEADD(day, (N*-7) - 1 - (DATEPART(dw, GETDATE()) + @@DATEFIRST - 2) % 7, GETDATE()) AS StartDate 
, DATEADD(day, (N*-7) + 5 - (DATEPART(dw, GETDATE()) + @@DATEFIRST - 2) % 7, GETDATE()) AS EndDate 
FROM GetN(10)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:50:22+00:00Added an answer on May 25, 2026 at 11:50 pm

    Here’s a way using a recursive CTE:

    WITH 
        TestData as (
            SELECT 1 as N
            UNION ALL
            SELECT N + 1 FROM TestData WHERE N < 10
        )
    SELECT 
        N,
        DATEPART(wk, GETDATE())-N as WeekNumber,
        DATEADD(day, (N*-7) - 1 - (DATEPART(dw, GETDATE()) + @@DATEFIRST - 2) % 7, GETDATE()) AS StartDate,
        DATEADD(day, (N*-7) + 5 - (DATEPART(dw, GETDATE()) + @@DATEFIRST - 2) % 7, GETDATE()) AS EndDate
    FROM
        TestData
    

    The “count” is specified by the “N < 10” part at the end of the second SELECT.

    Prints out:

    N   WeekNumber  StartDate   EndDate
    1   39  2011-09-18 15:24:40.800 2011-09-24 15:24:40.800
    2   38  2011-09-11 15:24:40.800 2011-09-17 15:24:40.800
    3   37  2011-09-04 15:24:40.800 2011-09-10 15:24:40.800
    4   36  2011-08-28 15:24:40.800 2011-09-03 15:24:40.800
    5   35  2011-08-21 15:24:40.800 2011-08-27 15:24:40.800
    6   34  2011-08-14 15:24:40.800 2011-08-20 15:24:40.800
    7   33  2011-08-07 15:24:40.800 2011-08-13 15:24:40.800
    8   32  2011-07-31 15:24:40.800 2011-08-06 15:24:40.800
    9   31  2011-07-24 15:24:40.800 2011-07-30 15:24:40.800
    10  30  2011-07-17 15:24:40.800 2011-07-23 15:24:40.800
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might be a confusing question but I have written below a Directory crawler,
I have written grammar for a language (sample code below) //this is a procedure
I have written this query for retrieving data from mysql as below select FeedbackCode,EMailID,FeedbackDetail,
I have been wondering which is the best way to load a navigation view.
I have written below code to check for blank value in my textbox but
I have written the below code on my view page; @Html.CheckBox(ChxName,true) and I got
I have filled the listview using the code given below, but I'm unablee to
i have written the below code to display the AlertDialogBox as follows private void
I have written a function below: void trans(double x,double y,double theta,double m,double n) {
I have written the batch file below, in order to automate the process of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.