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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:50:42+00:00 2026-06-11T22:50:42+00:00

I have a table with the following fields: Reports (table name) Rep_Date (date) Rep_Time

  • 0

I have a table with the following fields:

Reports (table name)
Rep_Date (date)
Rep_Time (date)

The Rep_Time field has values like ’01/01/1753 07:30:00′ i.e. the time part is relevant. I have written the following query:

select Reports.pid, MaxDate from Reports
INNER JOIN (
    select pid, max(TO_DATE(TO_CHAR(REP_DATE, 'DD/MM/YYYY')
        || TO_CHAR(REP_TIME, 'HH24:MI:SS'), 'DD/MM/YYYY HH24:MI:SS')) As MaxDate
    from reports
    group by pid
) ReportMaxDate
on Reports.PID = ReportMaxDate.PID
AND To_Date(To_Char(MaxDate, 'DD/MM/YYYY')) = REP_DATE
WHERE REPORTS.PID=61

The derived table part of the query runs, but when I run the entire query I get an error: “not a valid month”. Why is this?

In order to help debug this; if I run the following query:

select rep_date, rep_time from reports where pid=61 and rownum=1

I get:

Rep_Date = 01/04/2009
Rep_Time = 01/01/1753 13:00:00

UPDATE 15:58
I am now able to execute the following query:

select Reports.pid, MaxDate from Reports
INNER JOIN (
    select pid, max(TO_DATE(TO_CHAR(REP_DATE, 'DD/MM/YYYY')
        || TO_CHAR(REP_TIME, 'HH24:MI:SS'), 'DD/MM/YYYY HH24:MI:SS')) As MaxDate
    from reports group by pid
) ReportMaxDate
on Reports.PID = ReportMaxDate.PID
AND to_date(to_char(maxdate,'MM/DD/YYYY'),'MM/DD/YYYY') = REP_DATE
WHERE REPORTS.PID=61

However, I need to add one more statement to the WHERE clause comparing the time part of MaxDate to rep_time: to_date(to_char(maxdate,'MM/DD/YYYY'),'MM/DD/YYYY') = REP_DATE does not work.

  • 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-11T22:50:44+00:00Added an answer on June 11, 2026 at 10:50 pm

    1.

    To_Date(To_Char(MaxDate, 'DD/MM/YYYY')) = REP_DATE
    

    is causing the issue. when you use to_date without the time format, oracle will use the current sessions NLS format to convert, which in your case might not be “DD/MM/YYYY”. Check this…

    SQL> select sysdate from dual;
    
    SYSDATE
    ---------
    26-SEP-12
    
    Which means my session's setting is DD-Mon-YY
    
    SQL> select to_char(sysdate,'MM/DD/YYYY') from dual;
    
    TO_CHAR(SY
    ----------
    09/26/2012
    
    
    SQL> select to_date(to_char(sysdate,'MM/DD/YYYY')) from dual;
    select to_date(to_char(sysdate,'MM/DD/YYYY')) from dual
                   *
    ERROR at line 1:
    ORA-01843: not a valid month
    
    SQL> select to_date(to_char(sysdate,'MM/DD/YYYY'),'MM/DD/YYYY') from dual;
    
    TO_DATE(T
    ---------
    26-SEP-12
    

    2.

    More importantly, Why are you converting to char and then to date, instead of directly comparing

    MaxDate = REP_DATE
    

    If you want to ignore the time component in MaxDate before comparision, you should use..

    trunc(MaxDate ) = rep_date
    

    instead.

    ==Update : based on updated question.

    Rep_Date = 01/04/2009 Rep_Time = 01/01/1753 13:00:00
    

    I think the problem is more complex. if rep_time is intended to be only time, then you cannot store it in the database as a date. It would have to be a string or date to time interval or numerically as seconds (thanks to Alex, see this) . If possible, I would suggest using one column rep_date that has both the date and time and compare it to the max date column directly.

    If it is a running system and you have no control over repdate, you could try this.

    trunc(rep_date) = trunc(maxdate) and 
    to_char(rep_date,'HH24:MI:SS') = to_char(maxdate,'HH24:MI:SS')
    

    Either way, the time is being stored incorrectly (as you can tell from the year 1753) and there could be other issues going forward.

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

Sidebar

Related Questions

I have a table called Employee with the following fields: EmpID Salary Name I
I have a table in db2 which has the following fields int xyz; string
I have a table that has multiple rows with the following fields: PersonName SongName
I have 2 tables. tb_Employees tb_Orders tb_Employees has the following fields empID name tb_Orders
I have a table with the following fields: email - name - username -
I have mysql table with following fields id (which auto increments) title,name,age,institution,iod the fields
I have a table in a SQL database with the following fields: ID, Name,
A Clarification, if I have a table that has the following fields.... ProductCategoryID, CategoryName,
I have a table which has following fields project_name VARCHAR (100) running_frequency enum ('daily',
I have a SQL table with following fields ID, Rly, Shed, Loco_no, shed, Date_failure,

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.