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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:57:17+00:00 2026-06-03T01:57:17+00:00

I have a weird behavior going on here, and I’m hoping someone can explain

  • 0

I have a weird behavior going on here, and I’m hoping someone can explain it to me.

I have two fields in a query. One is a numeric field that is converted to a date by taking to_date('01/01/1960', 'mm/dd/yyyy') + somethingorother. Another is a text field, which includes at least one non-date value, that is converted to a date with to_date(textField, 'mm/dd/rrrr'). If I run the query, it runs fine. However, if I enclose the query in select * from ( ) where field1 > field2, it blows up with an “ORA-01861: literal does not match format string” error. If I try excluding the known non-date value in the where clause of the subquery that pulls back the text field, it doesn’t help.

I know this is almost impossible to figure out without code, but I’m wondering if anyone can explain to me why it works without the filter, but blows up when I add it. Thanks.

  • 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-03T01:57:19+00:00Added an answer on June 3, 2026 at 1:57 am

    The general problem is that since SQL is a set-based language, Oracle is free to evaluate your predicates in any order it chooses. If you have a VARCAHR2 column that stores some date values and some non-date values, that means that Oracle is free to either evaluate the predicate that filters out all of the non-date values first or to evaluate the predicate that checks whether one of the converted DATE values is greater than the other first. If it happens to evaluate the DATE inequality predicate (field1 > field2) before filtering out the non-date values, you’ll get an error.

    The fact that SQL is set-based is one of the major reasons that using the wrong data type is so problematic– you can’t ever be certain that a query is always going to filter out the non-convertable data before calling the conversion function. Even if you set up abstraction barriers like views that filter out the invalid data, the optimizer is free to reorder predicates so you can easily find that your queries end up breaking your abstraction barriers or that you have queries that work most of the time unless the optimizer happens to pick a different execution plan. Jonathan Gennick has a very enjoyable article Subquery Madness that talks about this specific issue.

    You can write your own conversion function that ignores the exceptions and use that in your query. For example, you can create a function

    CREATE OR REPLACE FUNCTION my_to_date( p_date_str    IN VARCHAR2,
                                           p_format_mask IN VARCHAR2 )
      RETURN DATE
    IS
      l_date DATE;
    BEGIN
      l_date := to_date( p_date_Str, p_format_mask ); 
      RETURN l_date;
    EXCEPTION
      WHEN OTHERS THEN
        RETURN null;
    END;
    

    and then use that function in your query

    SELECT *
      FROM (SELECT to_date('01/01/1960', 'mm/dd/yyyy') + somethingorother field1,
                   my_to_date( textField, 'mm/dd/rrrr' ) field2
              FROM your_table
             WHERE some_condition)
     WHERE field1 > field2
    

    That will work because it will be valid to call my_to_date on any string whether or not it evaluates to a valid DATE so your query no longer depends on the order in which Oracle chooses to evaluate the predicates.

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

Sidebar

Related Questions

We have a weird situation going on here. We have setup a local installation
I have a weird date rounding problem that hopefully someone can solve. My client
I have a very strange behavior in going on KnockoutJS 1.2.1 (Can't switch to
I have a weird behavior I can't pinpoint the source of. I have my
I have no idea what's going on here. I'm using R2006b. Any chance someone
I have stumbled on a weird behavior that I just could not explain at
I'm fairly new to SVN and I have a weird behavior issue. I'm working
Weird behavior when mixing loading of assemblies using Assembly.LoadFrom and Assembly.Load : I have
This is the day of weird behavior. We have a Win32 project made with
I am getting a weird behavior. I have a class that I created that

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.