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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:16:26+00:00 2026-05-28T06:16:26+00:00

I am trying to refactor some code in an ASP.Net website and having a

  • 0

I am trying to refactor some code in an ASP.Net website and having a problem with a stored procedure I am writing.

What I want to do is get a date range, then select all data within that range from a table BUT if a date is not present I need to still select a row.

My idea for this as you can see in the code below is to create a temporary table, and populate it with all the dates within my date range, then join this onto the table I am selecting from however this does not work. Am I doing something wrong here? The tempDate column is always null in this join however I have checked the table and it deffinately has data in it.

-- Parameters
DECLARE @DutyDate datetime='2012-01-01 00:00:00'
DECLARE @InstructorID nvarchar(2) = N'29'

DECLARE @datesTBL TABLE (tempDate DATETIME)

-- Variables
DECLARE @StartDate DATETIME 
DECLARE @EndDate DATETIME

SELECT 
    @StartDate =StartDate, 
    @EndDate = EndDate
FROM 
    DutyPeriodTbl 
WHERE 
(StartDate <= @DutyDate) 
AND 
(EndDate >= @DutyDate)


DECLARE @d DATETIME = @StartDate 
WHILE @d<=@EndDate
BEGIN
    INSERT INTO @datesTBL VALUES (CONVERT(DATETIME, @d, 102))
    SET @d=DATEADD(day,1,@d)
END

SELECT 
    dt.tempDate ,
    InstructorID,           EventStart, 
    EventEnd,               cancelled, 
    cancelledInstructor, 
    EventType,              DevName, 
    Room,                   SimLocation, 
    ClassLocation,          Event, 
    Duration,               TrainingDesc, 
    Crew,                   Notes, 
    LastAmended,            InstLastAmended, 
    ChangeAcknowledged,     Type, 
    OtherType,              OtherTypeDesc, 
    CourseType 
FROM 
    OpsInstructorEventsView iv
LEFT OUTER JOIN
    @datesTBL dt 
ON
    CONVERT(DATETIME, iv.EventStart, 102) = CONVERT(DATETIME, dt.tempDate, 102) 
WHERE 
    InstructorID = @InstructorID 
AND 
    EventStart BETWEEN CONVERT(DATETIME, @StartDate, 102) AND CONVERT(DATETIME, @EndDate, 102)
ORDER BY 
    EventStart 
  • 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-28T06:16:27+00:00Added an answer on May 28, 2026 at 6:16 am

    There are several ways of dealing with missing rows, but all are about having another set of data to combine with your current results.

    That could be derived from your results, created by a CTE or other process (such as your example), or (my preference) by using a permanent template to join against.

    The template in your case could just be a table of dates, like your @datesTBL. The difference being that it’s created in advance with, for example, 100 years worth of dates.

    Your query may then be similar to your example, but I would try the following…

    SELECT 
        dt.tempDate ,
        InstructorID,           EventStart, 
        EventEnd,               cancelled, 
        cancelledInstructor, 
        EventType,              DevName, 
        Room,                   SimLocation, 
        ClassLocation,          Event, 
        Duration,               TrainingDesc, 
        Crew,                   Notes, 
        LastAmended,            InstLastAmended, 
        ChangeAcknowledged,     Type, 
        OtherType,              OtherTypeDesc, 
        CourseType 
    FROM 
      @datesTBL dt 
    LEFT OUTER JOIN
      OpsInstructorEventsView iv
        ON  iv.EventStart >= dt.tempDate
        AND iv.EventStart <  dt.tempDate + 1
        AND iv.InstructorID = @InstructorID 
    WHERE
          dt.tempDate >= @StartDate
      AND dt.tempDate <= @EndDate
    ORDER BY
      dt.tempDate,
      iv.EventStart
    

    This puts the calendar template on the LEFT, and so makes many queries easier as you know the calendar’s date field is always populated, is always a date only (no time part) value, is in order, is simple to GROUP BY, etc.

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

Sidebar

Related Questions

I am trying to refactor some code while leaving existing functionality in tact. I'm
I'm trying to refactor some code and move some of my before_filter's from the
I'm quite new to smart pointers and was trying to refactor some existing code
I'm trying to refactor some of my code and i'm wondering if it is
I'm trying to refactor some of our Script# code to reduce duplicate code. I'm
I'm new to javascript and was trying to refactor some code, clearly there's something
I'm trying to refactor some legacy code to use Spring to handle the jms
I'm trying to refactor some python code and I'm stuck with an import error
I'm trying to refactor some code here that was done previously by other guys,
Im currently trying to refactor a project(asp.net mvc) that doesnt have any separation at

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.