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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:10:24+00:00 2026-06-02T04:10:24+00:00

I have the DateFirst set to 7. Now when I run the following SQL,

  • 0

I have the DateFirst set to 7. Now when I run the following SQL, it returns week 1:

SET DateFirst 7
SELECT DATEPART(wk, '1/1/2011') as WeekNumber

I am not sure why this is returning week 1 though, if my start day is set to 7. According to me week 1 should start on the 1/2/2011, which is a Sunday. How can I correct this?

  • 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-02T04:10:27+00:00Added an answer on June 2, 2026 at 4:10 am

    From http://msdn.microsoft.com/en-us/library/ms174420%28v=sql.90%29.aspx

    The week (wk, ww) datepart reflects changes made to SET DATEFIRST.
    January 1 of any year defines the starting number for the week
    datepart, for example: DATEPART(wk, ‘Jan 1, xxxx’) = 1, where xxxx is
    any year.

    So: because it is defined as such.

    Edit: You can fix this within SQL-SERVER. Use something like this:

    DATEPART(wk, 
      DATEADD(day, 
        -( (DATEPART(day, date) + 8 - DATEPART(weekday, date)) % 7 - 1),
        date
      )
    )
    

    What this code does, is use the current day of the year and the weekday to calculate which day was the first Sunday of the year, then subtracts that day from the current date, giving you a virtual date in which the year started on a Sunday and then takes the week number of that date, which should be equal to the week number according to your definition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently doing integration testing on a database and I have the following sql
Have the following cronjob set up in root's crontab: (centos 5.x) 2 * *
How can I run MSSQL .sql files with php? I have a collection of
I have a PHP application which runs a large set of SQL files. It
I have run across an issue in a query using datepart. We have been
I have following SQL. I need to convert it to LINQ. ALTER VIEW [dbo].[vwRptBorrowerAccount]
I have this SQL statement: SELECT * FROM converts WHERE email='myemail@googlemail.com' AND status!='1' ORDER
I have an SQL query from SQL Server which returns dates as a string
Have a matrix report now that has Position, Hours and Wages for a location
have not tested on windows. but in ubuntu when u disconnect from the network,

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.