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

  • Home
  • SEARCH
  • 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 85303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:04:05+00:00 2026-05-10T22:04:05+00:00

Is it possible to create an sql statement that selects the week number (NOT

  • 0

Is it possible to create an sql statement that selects the week number (NOT the day of week – or the day number in a week). I’m creating a view to select this extra information along with a couple of other fields and thus can not use a stored procedure. I’m aware that it’s possible to create a UDF to do the trick, but if at all possible i’d rather only have to add a view to this database, than both a view and a function.

Any ideas? Also where i come from, the week starts monday and week 1 is the first week of the year with atleast 4 days.

Related:

How do I calculate the week number given a date?

  • 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. 2026-05-10T22:04:06+00:00Added an answer on May 10, 2026 at 10:04 pm

    Be aware that there are differences in what is regarded the correct week number, depending on the culture. Week numbers depend on a couple of assumptions that differ from country to country, see Wikipedia article on the matter. There is an ISO standard (ISO 8601) that applies to week numbers.

    The SQL server integrated DATEPART() function does not necessarily do The Right Thing. SQL Server assumes day 1 of week 1 would be January 1, for many applications that’s wrong.

    Calculating week numbers correctly is non-trivial, and different implementations can be found on the web. For example, there’s an UDF that calculates the ISO week numbers from 1930-2030, being one among many others. You’ll have to check what works for you.

    This one is from Books Online (though you probably want to use the one from Jonas Lincoln’s answer, the BOL version seems to be incorrect):

    CREATE FUNCTION ISOweek  (@DATE DATETIME) RETURNS INT AS BEGIN    DECLARE @ISOweek INT    SET @ISOweek = DATEPART(wk,@DATE)                    +1                    -DATEPART(wk,CAST(DATEPART(yy,@DATE) AS CHAR(4))+'0104')    -- Special cases: Jan 1-3 may belong to the previous year    IF (@ISOweek=0)       SET @ISOweek = dbo.ISOweek(CAST(DATEPART(yy,@DATE) - 1                      AS CHAR(4))+'12'+ CAST(24+DATEPART(DAY,@DATE) AS CHAR(2)))+1    -- Special case: Dec 29-31 may belong to the next year    IF ((DATEPART(mm,@DATE)=12) AND       ((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28))       SET @ISOweek=1    RETURN(@ISOweek) END GO 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 51k
  • Answers 51k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Because Visual Studio has to process the aspx files to… May 11, 2026 at 6:21 am
  • added an answer AFAIK, long long isn't supported in MSVC 6.0. For example,… May 11, 2026 at 6:21 am
  • added an answer I use Microsoft Communities. They are all listed on http://www.microsoftcommunities.com/.… May 11, 2026 at 6:21 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.