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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:47:45+00:00 2026-05-23T20:47:45+00:00

I have quite tough task for me to do in T-SQL (MSSQL 2005). I

  • 0

I have quite tough task for me to do in T-SQL (MSSQL 2005). I have a table like this:

WeekDay| SlotTime
------------------
|  1   |   07:00
|  3   |   09:00
|  7   |   14:00
|  1   |   15:00
|  4   |   22:00
|  6   |   08:00

where 1st column is WeekDay number and 2nd column is some Time value.

As parameters for my query I have 2 dates, for example:

StartDate = '2011-07-20'
EndDate = '2011-08-17'

This is a range definition for my data. I have to generate for these range all dates where WeekDay (from table above) happens and add to them SlotTime value.
So for example, for above dates range result column should be:

2011-07-20 9:00
2011-07-21 22:00
2011-07-23 8:00
2011-07-24 14:00
2011-07-25 7:00
2011-07-25 15:00
2011-07-27 9:00
2011-07-28 22:00
2011-07-30 8:00
etc.
...

Any idea how to achieve this? Any tips? 🙂 I’m considering this quite impossible without some huge(?) calculations and additional tables…

Edit (maybe this snippet will help)
I was playing with this function to use it as part of my calculations but was unable to achieve my goal. Maybe some part of this can be used in final solution…

create function dbo.NthWeekDay(
   @first datetime,   -- First of the month of interest (no time part)
   @nth tinyint,      -- Which of them - 1st, 2nd, etc.
   @dow tinyint       -- Day of week we want
) returns datetime as begin
-- Note: Returns a date in a later month if @nth is too large
  declare @result datetime
  set @result = @first + 7*(@nth-1)
  return @result + (7 + @dow - datepart(weekday,@result))%7
end
go

SET DATEFORMAT ymd
SET DATEFIRST 1

select dbo.NthWeekDay('2011-07-20',1,1) as D

go

drop function NthWeekDay 
  • 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-23T20:47:46+00:00Added an answer on May 23, 2026 at 8:47 pm

    This will do the trick

    SET DATEFIRST 1 
    -- temp table
    declare @t table(WeekDay tinyint, SlotTime time)
    -- fill table
    insert @t values (1, '7:00')
    insert @t values (3, '9:00')
    insert @t values (7, '14:00')
    insert @t values (1, '15:00')
    insert @t values (4, '22:00')
    insert @t values (6, '8:00')
    
    -- declare interval
    declare @startdate datetime
    declare @enddate   datetime
    set @StartDate = '2011-07-20'
    set @EndDate   = '2011-08-17'
    
    ;with cte as
    (
    -- recusive to make timeline
    SELECT @StartDate loopday
    UNION ALL
    SELECT loopday + 1
    FROM cte
    WHERE loopday  < @EndDate 
    ), b as
    (
    -- join timeline with Weekday and add Slottime to timeline
    SELECT loopday + t.SlotTime col
    FROM cte
    JOIN @t t
    ON t.WeekDay = datepart(weekday, cte.loopday)
    )
    SELECT col 
    FROM b
    ORDER BY 1
    OPTION( MAXRECURSION 0)
    

    (Result looks like your output)

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

Sidebar

Related Questions

I have quite a few RewriteRules in my .htaccess that looks like this RewriteRule
I have quite a complicated ListView . Each item looks something like this: >
I have quite a few situations where I have database structures similar to: TABLE
I have quite a 'heavy' python program that I would like compiled to an
I have quite simple task to accomplish - I have to set up the
I'm working this programming task for college where we have to write a c++
I have a text file with hundreds of entries formatted like this, I need
I have found quite a few questions related to this but I haven't found
I have quite a large list of words in a txt file and I'm
I have quite a few cells that link to other worksheets/books, etc with standard

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.