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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:37:59+00:00 2026-06-18T17:37:59+00:00

I want to get the right week number. Using the to_char function, the function

  • 0

I want to get the right week number.

Using the to_char function, the function enumerate the week starting with mondey, but i want to get the week number considering that a week start on saturday.

Here is an example:

SELECT to_char('05-01-2013'::date,'daydd-mm-yyyy') as date_char, to_char('05-01-2013'::date,'IW') as week_number

Result:

"saturday 05-01-2013";"01"   

And should be:

"saturday 05-01-2013";"02"  

Is there any way to get it?

Config info: Postgresql 9.2 under Windows 7

  • 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-18T17:38:00+00:00Added an answer on June 18, 2026 at 5:38 pm

    This will give the correct result:

    with first_friday as (
        select extract(doy from min(a)::date) ff
        from generate_series('2013-01-01'::date, '2013-01-07', '1 day') s(a)
        where extract(dow from a) = 5
    )
    select
        to_char(a, 'day'),
        a::date "day",
        floor((extract(doy from a) - (select ff from first_friday) - 1) / 7) + 2 week_number
    from generate_series('2013-01-01'::date, '2013-01-31', '1 day') s(a)
    ;
      to_char  |    day     | week_number 
    -----------+------------+-------------
     tuesday   | 2013-01-01 |           1
     wednesday | 2013-01-02 |           1
     thursday  | 2013-01-03 |           1
     friday    | 2013-01-04 |           1
     saturday  | 2013-01-05 |           2
     sunday    | 2013-01-06 |           2
     monday    | 2013-01-07 |           2
     tuesday   | 2013-01-08 |           2
     wednesday | 2013-01-09 |           2
     thursday  | 2013-01-10 |           2
     friday    | 2013-01-11 |           2
     saturday  | 2013-01-12 |           3
     sunday    | 2013-01-13 |           3
     monday    | 2013-01-14 |           3
     tuesday   | 2013-01-15 |           3
     wednesday | 2013-01-16 |           3
     thursday  | 2013-01-17 |           3
     friday    | 2013-01-18 |           3
     saturday  | 2013-01-19 |           4
     sunday    | 2013-01-20 |           4
     monday    | 2013-01-21 |           4
     tuesday   | 2013-01-22 |           4
     wednesday | 2013-01-23 |           4
     thursday  | 2013-01-24 |           4
     friday    | 2013-01-25 |           4
     saturday  | 2013-01-26 |           5
     sunday    | 2013-01-27 |           5
     monday    | 2013-01-28 |           5
     tuesday   | 2013-01-29 |           5
     wednesday | 2013-01-30 |           5
     thursday  | 2013-01-31 |           5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a function to get the right week number of year.
I want to get this image uploading script right, but for whatever reason it
I have just started using c++ exceptions and want to get it right. What
I tried but could not get the right solution. I want an SQL query
I want get all of the Geom objects that are related to a certain
Have next function to get week of year: static public Integer getWeek(Date date) {
Last week i read about liquibase quick start and all the related tutorials but
I've researched that using global is considered bad practice as it allows the function
I want get the time used for a case so I can create an
I want get as much as possible from Redis + Hiredis + libevent. I'm

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.