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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:09:45+00:00 2026-05-24T02:09:45+00:00

There is a datepart iso_week returning the iso_week for a selected day. SELECT DATEPART(iso_week,

  • 0

There is a datepart iso_week returning the iso_week for a selected day.

SELECT DATEPART(iso_week, getdate())

This will return the european standard for week current. It is not nessasarily the same as week.

Here comes the tricky part:

SELECT DATEPART(iso_week, '2011-01-01')

Returns

52 

so it belongs to last year
In order to group by year and iso_week, I need to consider that iso_week is not from the same year.

Iso_week starts on monday and belongs to the year where most days overlap. So 4 days in 2010 and 3 days in 2011, the week is 52 and all days belong to iso_year 2010. However TSQL doesn’t have the detepart iso_year.

declare @t table(col1 datetime)

insert @t 
ALL SELECT '2010-12-28'
UNION ALL SELECT '2010-12-29'
UNION ALL SELECT '2010-12-30'
UNION ALL SELECT '2010-12-31'
UNION ALL SELECT '2011-01-01'
UNION ALL SELECT '2011-01-02'
--UNION ALL SELECT '2011-01-03' Stackexchange is bugged and can't handle this line showing 53
UNION ALL SELECT '2011-01-04'

I need something like (iso_year doesn’t exist):

SELECT DATEPART(ISO_WEEK, col1) WEEK, DATEPART(iso_YEAR, col1) YEAR, COUNT(*) COUNT 
FROM @t
GROUP BY DATEPART(ISO_WEEK, col1), DATEPART(iso_YEAR, col1)
ORDER BY  2,1

Expected result

WEEK  YEAR          COUNT
52    2010           6
1     2011           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-05-24T02:09:46+00:00Added an answer on May 24, 2026 at 2:09 am

    The same ISO week’s Thursday will unambiguously provide you with the correct year. And this answer can provide you with the idea how to obtain the right Thursday from the given date.

    SELECT
      Week  = DATEPART(ISOWK, TheThursday),
      Year  = DATEPART(YEAR, TheThursday),
      Count = COUNT(*)
    FROM (
      SELECT
        TheThursday = DATEADD(
          DAY,
          3 - (DATEPART(DW, col1) + @@DATEFIRST - 2) % 7,
          col1
        )
      FROM @t
    ) s
    GROUP BY
      TheThursday
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There's a great code sample in this post: DateTimePicker automatically move to next datepart
There is any syntax to use something like this?: <?php function get_foo() { return
Is there a quick one-liner to call datepart in Sql Server and get back
There is previous little on the google on this subject other than people asking
Is there a way to pass the DatePart parameter of DateDiff as a variable?
If i use DatePart(m,Now()) ' out put will be 7 but my requirment is
When I do for example DATEPART(mm, GETDATE()) I get the result of the month
is there any way to use defined column name in SELECT statement with WHERE
is there any way to use defined column name in SELECT statement with WHERE
CREATE VIEW xxxx AS SELECT order_date, DATEPART(W, order_date) AS dayID, Type FROM dbo.Shipment_Dates WHERE

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.