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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:29:42+00:00 2026-06-06T03:29:42+00:00

my first time posting. I have a tricky task of finding the latest date

  • 0

my first time posting.

I have a tricky task of finding the latest date within a range, but excluding multiple other date ranges. I have code that does work, but it seems awfully taxing.

I am selecting the MAX(Date) within a range. However, I have a table, bfShow, where each show has its own date-range (stored as DateStart and DateEnd). So I need the MAX(Date) within the range which does NOT have a show on that date (there may be 0 to 99 shows overlapping my date-range).

Note: I have dbo.fnSeqDates which works great (found via Google) and returns all dates within a range – makes for very fast filling in 6/1/12, 6/2/12, 6/3/12…6/30/12, etc.

What I’m doing (below) is creating a table with all the dates (within range) in it, then find all the Shows within that range (#ShowIDs) and iterate through those shows, one at a time, deleting all those dates (from #DateRange). Ultimately, #DateRange is left with only “empty” dates. Thus, the MAX(Date) remaining in #DateRange is my last date in the month without a show.

Again, my code below does work, but there’s got to be a better way. Thoughts?

Thank you,
Todd


CREATE procedure spLastEmptyDate
    @DateStart date
  , @DateEnd   date
as
begin

   -- VARS...
   declare @ShowID    int
   declare @EmptyDate date

   -- TEMP TABLE...
   create table #DateRange(dDate date)
   create table #ShowIDs(ShowID int)

   -- LOAD ALL DATES IN RANGE (THIS MONTH-ISH)...
   insert into #DateRange(dDate)
   select SeqDate
   from dbo.fnSeqDates(@DateStart, @DateEnd)

   -- LOAD ALL SHOW IDs IN RANGE (THIS MONTH-IS)...
   insert into #ShowIDs(ShowID)
   select s.ShowID
   from bfShow s
   where s.DateStart = @DateStart

   -- PRIME SHOW ID...
   set @ShowID = 0
   select @ShowID = min(ShowID)
   from #ShowIDs

   -- RUN THRU ALL, REMOVING DATES AS WE GO...
   while (@ShowID > 0)
   begin
      -- REMOVE FROM TEMP...
      delete DR
      from #DateRange DR
         , bfShow s
      where DR.dDate between s.DateStart and s.DateEnd
        and s.ShowID = @ShowID

      -- DROP THAT ONE FROM TEMP...
      delete from #ShowIDs
      where ShowID = @ShowID

      -- GET NEXT ID...
      set @ShowID = 0
      select @ShowID = min(ShowID)
      from #ShowIDs
   end

   -- GET LAST EMPTY SPOT...
   select @EmptyDate = max(dDate)
   from #DateRange

   -- CLEAN UP...
   drop table #DateRange
   drop table #ShowIDs

   -- RETURN DATA...
   select @EmptyDate as LastEmptyDateInRange

end
  • 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-06T03:29:43+00:00Added an answer on June 6, 2026 at 3:29 am

    Let us know what version of SQL Server you’re on because that will help determine your options, but you should be able to use the BETWEEN operator in a JOIN between the fnSeqDates function (it’s a table-valued function, so you can join to it directly rather than inserting them into a temp table) and the bfShow tables:

    SELECT TOP 1 tDate.SeqDate
    FROM dbo.fnSeqDates('6/1/2012', '6/30/2012') tDate
        LEFT JOIN bfShow tShow
            ON tDate.SeqDate BETWEEN tShow.DateStart AND tShow.DateEnd
    WHERE tShow.ShowID IS NULL -- no matches found
    ORDER BY tDate.SeqDate DESC -- to pull the most recent date
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first time posting to stackoverflow, but these threads have helped me
this is my first time posting here, I have a question which I have
first time posting here. I'm using a loop to create 3 buttons, but my
So this is my first time posting here but I'm more or less stumped.
That's my first time posting on stackoverflow. I've been finding usefull answers on this
this is my first time posting on StackOverflow but this site is awesome. Thanks
use this website a lot but first time posting. My program creates a number
this is my first time posting on here and have read alot of helpful
Hey guys, this is my first time actually posting at stackflow but i've used
this is my first time posting here, but I didn't know where else to

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.