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 8187119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:27:25+00:00 2026-06-07T02:27:25+00:00

The time is: (m/d/yyyy) => 2009/01/04 Using this command using datepart(wk,’20090104′) I can get

  • 0

The time is: (m/d/yyyy) => 2009/01/04

enter image description here

Using this command using datepart(wk,'20090104') I can get the week number (for any given date).

So :

SELECT datepart(wk,'20090101') //1
SELECT datepart(wk,'20090102') //1
SELECT datepart(wk,'20090103') //1
SELECT datepart(wk,'20090104') //2

So far so good.

The problem :

Those 3 first dates are not part of a full week, so I can’t put them in a fixed 52-week chart.

Our company needs to see information about each whole week in the 52 weeks of a year. (Each year has 52 whole weeks).

enter image description here

So 20090101 doesn’t belong to the first week of 2009 !

It belongs to the previous year (which is irrelevant to my question)

So I need a UDF (I’ve been searching a lot, and ISOWEEK is not answering my needs) which by a given datetime, will give me the Week Number (week = whole week, so partial weeks aren’t considered).

Example :

calcweekNum ('20090101') //52 ...from the last year
calcweekNum ('20090102') //52 ...from the last year
calcweekNum ('20090103') //52 ...from the last year
calcweekNum ('20090104') //1
..
..
calcweekNum ('20090110') //1
calcweekNum ('20090111') //2
calcweekNum ('20090112') //2
...
  • 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-06-07T02:27:27+00:00Added an answer on June 7, 2026 at 2:27 am

    Here’s a different approach. All you need to supply is the year:

    DECLARE @year INT = 2009;
    
    
    DECLARE @start SMALLDATETIME;
    SET @start = DATEADD(YEAR, @year-1900, 0);
    
    ;WITH n AS
    (
      SELECT TOP (366) -- in case of leap year
          d = DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY name)-1, @start)
        FROM sys.all_objects
    ),
    x AS 
    (
      SELECT md = MIN(d) FROM n 
        WHERE DATEPART(WEEKDAY, d) = 1 -- assuming DATEFIRST is Sunday
    ),
    y(d,wk) AS
    (
      SELECT n.d, ((DATEPART(DAYOFYEAR, n.d) - DATEDIFF(DAY, @start, x.md)-1)/7) + 1
      FROM n CROSS JOIN x
      WHERE n.d >= x.md
      AND n.d < DATEADD(YEAR, 1, @start)
    )
    SELECT [date] = d, [week] = wk
    FROM y WHERE wk < 53
    ORDER BY [date];
    

    Results:

    date        week
    ----------  ----
    2009-01-04  1
    2009-01-05  1
    2009-01-06  1
    2009-01-07  1
    2009-01-08  1
    2009-01-09  1
    2009-01-10  1
    2009-01-11  2
    2009-01-12  2
    ...
    2009-12-25  51
    2009-12-26  51
    2009-12-27  52
    2009-12-28  52
    2009-12-29  52
    2009-12-30  52
    2009-12-31  52
    

    Note that week 52 won’t necessarily be a full week, and that in some cases (e.g. 2012), the last day or two of the year might fall in week 53, so they’re excluded.

    An alternative approach is to repeat the MIN expression twice:

    DECLARE @year INT = 2009;
    
    
    DECLARE @start SMALLDATETIME;
    SET @start = DATEADD(YEAR, @year-1900, 0);
    
    ;WITH n AS
    (
      SELECT TOP (366) -- in case of leap year
          d = DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY name)-1, @start)
        FROM sys.all_objects
    ),
    y(d,wk) AS
    (
      SELECT n.d, ((DATEPART(DAYOFYEAR, n.d) - DATEDIFF(DAY, @start, (SELECT MIN(d) 
        FROM n WHERE DATEPART(WEEKDAY, d) = 1))-1)/7) + 1
      FROM n
      WHERE n.d >= (SELECT md = MIN(d) FROM n WHERE DATEPART(WEEKDAY, d) = 1)
      AND n.d < DATEADD(YEAR, 1, @start)
    )
    SELECT [date] = d, [week] = wk
    FROM y WHERE wk < 53
    ORDER BY d;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen this: how to get formatted date time like 2009-05-29 21:55:57 using javascript?
This is the user input: Date: DD-MM-YYYY Time: HH:SS Can anyone help me creating
This line System.DateTime.Parse(09/12/2009); convert date (string) to 9/12/2009 12:00:00 AM. How can I get
I have a NSString that is a date and time in this code: YYYY-MM-DD
I have a field with a date/time value like this: 2009-11-17 18:40:05 It's in
I have datetime strings that look can like any the following: 1 13 2009
I have a date in the following format MM-DD-YYYY How can I convert this
I'm trying to get the current time as a YYYY-MM-DD-HH-MM-SS formatted string in an
I want to convert this date ( 02-12-2010) mm-dd-yyyy to time format ie to
I basically need to get current date and time separately, formatted as: 2009-04-26 11:06:54

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.