Week Days generation

  • My question is only about Week Ending & Week # columns, they need to be generated by sql. Could some help me with the sql. If needed assume any fictitious table names. I just need some idea.

    Thanks in advance.

  • I use a calendar table for that. It's just a table of dates and the data about them.

    create table dbo.Calendar (

    CalendarDate datetime primary key,-- Date datatype in SQL 2008

    constraint CK_WholeDays check (CalendarDate = dateadd(day, datediff(day, 0, CalendarDate), 0), -- Only whole days, not needed if Date datetype on PK

    DayOfWeek tinyint,

    QuarterNumber tinyint,

    WorkDay bit not null,

    WeekEnding datetime not null references dbo.Calendar(CalendarDate), -- Could add constraints to make sure this matches a date with DayOfWeek = 7 or whatever

    ... other columns relevant to your business ... );

    Populate something like that, join to it, and you've got what you need. Has a ton of other uses, like workdays between two dates, end-dates/begin-dates for quarters, sorting by fiscal calendar month instead of actual calendar month, and so on.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • I am not actually creating a table. I am creating a report. I need sql to generate Week Ending and week # in a SSRS report not a table. They are not pulled from a table. They follow some sequence. Please help me. Thank you.

  • Does the SSRS report pull data from a query that connects to a database?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply