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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:46:21+00:00 2026-05-22T02:46:21+00:00

I have an sql query in which I need to select the records from

  • 0

I have an sql query in which I need to select the records from table where the time is between 3:00 PM yesterday to 3:00 PM today if today’s time is more than 3:00 PM.

If today’s time is less than that, like if today’s time is 1:00 PM. then then my query should take today’s time as 1:00 PM (which should return me records).

I need to get the time between 3:00pm yesterday to 3:00pm today if todays time is more than 3:00pm
if todays time is less than 3:00pm then get the 3:00pm yesterday to current time today

  • 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-22T02:46:22+00:00Added an answer on May 22, 2026 at 2:46 am

    The best way of handling this is to use an IF statement:

    IF TO_CHAR(SYSDATE, 'HH24') >= 15 THEN 
    
      SELECT x.*
        FROM YOUR_TABLE x
       WHERE x.date_column BETWEEN TO_DATE(TO_CHAR(SYSDATE -1, 'YYYY-MM-DD')|| ' 15:00:00', 'YYYY-MM-DD HH24:MI:SS')
                               AND TO_DATE(TO_CHAR(SYSDATE, 'YYYY-MM-DD')|| ' 15:00:00', 'YYYY-MM-DD HH24:MI:SS')
    
    ELSE
    
      SELECT x.*
        FROM YOUR_TABLE x
       WHERE x.date_column BETWEEN TO_DATE(TO_CHAR(SYSDATE -1, 'YYYY-MM-DD')|| ' 15:00:00', 'YYYY-MM-DD HH24:MI:SS')
                               AND SYSDATE
    
    END IF;
    

    Conditional WHERE clauses are non-sargable.

    Previously:

    If I understand correctly, you want to get records within the last day. If the current time is 3 PM or later, the time should be set to 3 PM. If earlier than 3 PM, take the current time…

    SELECT x.*
      FROM YOUR_TABLE x
      JOIN (SELECT CASE
                     WHEN TO_CHAR(SYSDATE, 'HH24') >= 15 THEN 
                      TO_DATE(TO_CHAR(SYSDATE, 'YYYY-MM-DD')|| ' 15:00:00', 'YYYY-MM-DD HH24:MI:SS')
                     ELSE SYSDATE
                   END AS dt
              FROM DUAL) y ON x.date_column BETWEEN dt - 1 AND dt
    

    Note:

    dt - 1 means that 24 hours will be subtracted from the Oracle DATE.

    Reference:

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

Sidebar

Related Questions

Can we have a SQL query which will basically help in viewing table and
I have a SQL SELECT query which has a LIKE clause containing an underscore,
I have this query in sql server 2000: select pwdencrypt('AAAA') which outputs an encrypted
consider the following example. I have to select all the records from table1 which
I have a simple sql query that goes like this: SELECT * FROM leaderboard
I have an Informix SQL query which returns a set of rows. It was
I have SQL query, which is working nice on Oracle and MSSQL. Now I'm
Strange performance outcome, I have a LINQ to SQL query which uses several let
I have the following Linq to SQL query, in which I'm trying to do
I have the following query in iSeries SQL which I output to a file.

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.