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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:32:20+00:00 2026-06-17T04:32:20+00:00

I need to write a query that will figure out the previous/next day from

  • 0

I need to write a query that will figure out the previous/next day from today that has a record in the table, and then get all rows for that day.

When data is inserted, the days can differ. For example, just because it is tuesday does not mean the previous day is monday. The previous day could be sunday, saturday, or even wednesday of last week.

I’m trying to figure out how to best select the previous and next day (that is not today) that has a record. Then also, in the same query, get all of the rows for that day.

I don’t know if there is a function or anything for this, I’m pretty stumped. I know how to do this with 2 queries, but I want to do it with 1. Any help would be greatly appreciated.

  • 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-17T04:32:21+00:00Added an answer on June 17, 2026 at 4:32 am

    The following queries return the events in the nearest day, either in the past or in the future:

    select 'Nearest in the future';
    
    select * from job where schedule = (
      select schedule from job where schedule > date('now')
             order by schedule limit 1
    );
    
    select 'Nearest in the past';
    
    select * from job where schedule = (
      select schedule from job where schedule < date('now')
             order by schedule desc limit 1
    );
    

    Given the sample schema:

    create table if not exists job (
      id integer primary key not null,
      name text,
      schedule text
    );
    
    insert into job (name, schedule) values ( 'foo', date('now', '+32 days') );
    insert into job (name, schedule) values ( 'bar', date('now', '+12 days') );
    insert into job (name, schedule) values ( 'baz', date('now', '+12 days') );
    insert into job (name, schedule) values ( 'woo', date('now', '+55 days') );
    insert into job (name, schedule) values ( 'qoo', date('now', '-32 days') );
    insert into job (name, schedule) values ( 'bzz', date('now', '-18 days') );
    insert into job (name, schedule) values ( 'frr', date('now', '-18 days') );
    insert into job (name, schedule) values ( 'trr', date('now', '-55 days') );
    

    ORDER BY in conjuction with LIMIT 1 does the trick. Use > for the nearest in the future, and < for nearest in the past (and reverse the ordering). You may want to add indices on the schedule column to improve the performance.

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

Sidebar

Related Questions

I need to write a SQLlite query that will delete rows from a table
I need to write a query that will go through a table, find duplicate
Today I came across the need to write a CAML query that uses WHERE
I need to write a SQL query which will get me those rows from
I need to write a query that will return results if all the tables
I need to write a query that will generate a sort of sequenced ID
I need to write a query that will perform a keyword search on a
I need to write a SQL query that will generate a list of valid
I need to write a query that will group a large number of records
I need to write an NHibernate query that will place a restriction that accesses

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.