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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:39:58+00:00 2026-05-26T12:39:58+00:00

I want to compare data from the current week (1 through 52) to the

  • 0

I want to compare data from the current week (1 through 52) to the data from two weeks prior. I need to make sure that when the new-year boundary is crossed, the comparison keeps working. I have a working predicate, like this:

WHERE ( DATEPART(ww,MA.DateOpen) + 1 ) % 52 = DATEPART(ww,@ImportDate) - 1

The above logic works, but it does not account for proximate years. In other words, unwanted data from week ‘X’ of all prior years will be included in the comparison. So the predicate must be expanded to account for years, which in turn requires handling the new year boundary: week 1 of 2012 should be compared against week 51 of 2011.

How can this be done without a verbose looking predicate?

update:
This question is complicated by the need for “week of year” to be treated as Tuesday through Tuesday. My current “correct” attempt is embodied in the [VerboseCheck] case below.

SET DATEFIRST 2
DECLARE @ImportDate date = '2011-1-4'
DECLARE @DateOpen date = '2010-12-20'
select @ImportDate, 
YEAR(@DateOpen),
DATEPART(ww, @DateOpen),
YEAR(DATEADD(dd,-14,@ImportDate)), 
DATEPART(ww,DATEADD(dd,-14,@ImportDate)),
CASE WHEN 
  YEAR(@DateOpen) + DATEPART(ww, @DateOpen) = 
  YEAR(DATEADD(dd,-14,@ImportDate)) + DATEPART(ww,DATEADD(dd,-14,@ImportDate))
  THEN 1 ELSE 0 END [VerboseCheck],
CASE WHEN DATEDIFF (ww, @DateOpen, @ImportDate) =2
  THEN 1 ELSE 0 END [SimpleCheck]

The [SimpleCheck] would be perfect, but it gives a different answer than [VerboseCheck]. If I advance @DateOpen one more day, then the two give the same answer. This suggests that both are not honoring the DATEFIRST evaluation in the same way.

  • 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-26T12:39:58+00:00Added an answer on May 26, 2026 at 12:39 pm

    Seems like DateDiff would be best used here. However as you noted DATEFIRST is indeed not respected.

    So we just add Itzik Ben-Gan’s solution and we get

     WHERE DATEDIFF (ww, 
        DATEADD( day, -@@DATEFIRST , @DateOpen),
        DATEADD( day, -@@DATEFIRST , @ImportDate)
        ) =2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to compare two ms-access .mdb files to check that the data they
I have data from two spreadsheets where I need to compare the quantities of
I want to delete all data from a table that has a date prior
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I want to compare two arrays, one coming from a shoppingcart and the other
I am using BeautifulSoup to scrape data from a webpage. I want to compare
I want to take data from database and compare with system time.my android code
I want to compare two times and if the new time is more than
I've got two tables, and I want to compare the data between them, and
I have an array to which I need to compare data from mysql. Usually

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.