Why to_date doesn't work when results are filtered to match my date format (Oracle SQL)


Tom Bennett

I have a table 'A' with one column (VARCHAR2). The table contains one row containing the text "01/01/2021" and another row containing the text "A".

When I try to filter out "A" and then do a date update on the remaining values, I get "ORA-01858: non-numeric character found where a number was expected". I have tried both ways.

select *
from tbl
where col <> 'A'
and to_Date(col,'DD/MM/YYYY') = to_date('01/01/2020','DD/MM/YYYY');

select *
from (  select * 
        from tbl 
        where col <> 'A')
where to_Date(col,'DD/MM/YYYY') = to_date('01/01/2020','DD/MM/YYYY');

I can understand why the first method might not work, but in the second example, to_date should only see the filtered data (ie "01/01/2020").

When I delete the value of 'A', the statement runs and I get my results, so it seems to conclude that the reason it didn't run is because it was trying to update the value of 'A' even though that was supposed to be to Filter out then.

I've been able to replicate this file using the actual Oracle table, but unfortunately when I try to reproduce the table with WITH AS, the query works and no errors are encountered - another mystery!

Why doesn't this query work? The order of operations seems to be satisfactory (if I use WITH AS it works).

justin cave

Oracle (and other databases) are not obligated to evaluate predicates applied to inline views before evaluating outer predicates. In fact, often from a performance optimization standpoint, you want the optimizer to push select predicates from the outer query into the view, inline view, or subquery. In this case, whether the query throws an error will depend on the query plan chosen by the optimizer and which predicates it evaluates first.

As a quick trick, you can change the inline view to prevent the predicate from being pushed. In this case, the presence of a rownumprevents the optimizer from pushing the predicate. You can also use hints like no_push_predtrying to force the optimizer to use the plan you want

select *
from (  select t.*, rownum rn
        from tbl t
        where col <> 'A')
where to_Date(col,'DD/MM/YYYY') = to_date('01/01/2020','DD/MM/YYYY');

The problem with these two quick hacks, though, is that some future versions of the optimizer may have more options than you realize today, so you may run into problems in the future.

A better option is to rewrite the query so that you don't have to worry about the evaluation order of the predicates. In this case (depending on the Oracle version) this is very simple as it to_dateallows you to specify a value in the event of a conversion error

select *
from tbl
where col <> 'A'
and to_Date(col default null on conversion error,'DD/MM/YYYY') = 
      to_date('01/01/2020','DD/MM/YYYY');

If you're using an earlier version of Oracle, or to_datejust an example of a real problem, you can create a custom function that does the same thing.

create function safe_to_date( p_str in varchar2, p_fmt in varchar2 )
  return date
is
begin
  return to_date( p_str, p_fmt );
exception
  when value_error
  then
    return null;
end safe_to_date;
select *
from tbl
where col <> 'A'
and safe_to_date(col,'DD/MM/YYYY') = to_date('01/01/2020','DD/MM/YYYY');

Related


Why doesn't this DATE operator work for my query?

Tim: Can anyone explain to me why the query runs when trying to select data with dates greater than a certain date, but still see dates after 2020-09-01? Due_date is a Date and is stored in the MySQL database as YYYY-MM-DD. Select * from re

Oracle to_date SQL format issue,

User 4485029 I'm formatting a timestamp field into "DD/MM/YYYY" format to get all processed records for that date. select count(*) from task_log where to_date (event_dt,'DD/MM/YYYY')= to_date('19/05/2015','DD/MM/YYYY'); The above query returns 0 count. select

date format doesn't work

Sujwasham I have a month picker in Laravel 4.2. When changing the month, the month and year are displayed as 05/2015. But I need to be in May 2015 format. {{ HTML::style('public/asset/css/smoothness/jquery-ui.css') }} {{ HTML::style('public/asset/css/MonthPick

Why doesn't this DATE operator work for my query?

Tim: Can anyone explain to me why the query runs when trying to select data with dates greater than a certain date, but still see dates after 2020-09-01? Due_date is a Date and is stored in the MySQL database as YYYY-MM-DD. Select * from re

Why doesn't my date work in TypeScript?

Lord Wardmoot I am trying to initialize a Date object in the following way: let date = new Date(2020, 12, 5, 0, 0, 0, 0); then when i do date.getMonth() I get it 0. Can someone help me? and Oswald Months are zero-indexed, so the month to use for initializati

Why doesn't my Bootstrap date picker work?

Daniel I'm trying to use the Bootstrap datepicker ( https://github.com/eternicode/bootstrap-datepicker ), but I can't get it to work. On jsfiddle everything works like a charm : http://jsfiddle.net/hwuktpt2/ After some failed tests on the actual page, I even t

Date format doesn't work in some computers, SQL query

Shadrak Roulanwa The following date formats don't work on some computers, I do know why, SELECT FORMAT (getdate(), 'dd/MM/yyyy ') as date I use SQL query. what is the problem? please can anyone help me Shadrak Roulanwa I am trying to convert the variable inst

Why doesn't my date work in TypeScript?

Lord Wardmoot I am trying to initialize a Date object in the following way: let date = new Date(2020, 12, 5, 0, 0, 0, 0); then when i do date.getMonth() I get it 0. Can someone help me? and Oswald Months are zero-indexed, so the month to use for initializati

Why doesn't my date work in TypeScript?

Lord Wardmoot I am trying to initialize a Date object in the following way: let date = new Date(2020, 12, 5, 0, 0, 0, 0); then when i do date.getMonth() I get it 0. Can someone help me? and Oswald Months are zero-indexed, so the month to use for initializati

Why doesn't my date comparison work in JavaScript?

ic3man7019 I've researched this problem extensively and found some useful information, but haven't been able to solve my problem. All I want to do is parse one date and compare it to another date. Seems easy, right? Here is what I have tried: function getC

Oracle to_date SQL format issue,

User 4485029 I'm formatting a timestamp field into "DD/MM/YYYY" format to get all processed records for that date. select count(*) from task_log where to_date (event_dt,'DD/MM/YYYY')= to_date('19/05/2015','DD/MM/YYYY'); The above query returns 0 count. select

date format doesn't work

Sujwasham I have a month picker in Laravel 4.2. When changing the month, the month and year are displayed as 05/2015. But I need to be in May 2015 format. {{ HTML::style('public/asset/css/smoothness/jquery-ui.css') }} {{ HTML::style('public/asset/css/MonthPick

SQL query doesn't show results when querying with date

RDPD I see a table with data values as 18-May-2012. However, when I look for the same date using the following query query, no results are available. Select Submit_Dt From Siebel.S_Order_Dtl where submit_dt = '18-May-2012' Can you help me with this problem? u

Why doesn't this DATE operator work for my query?

Tim: Can anyone explain to me why the query runs when trying to select data with dates greater than a certain date, but still see dates after 2020-09-01? Due_date is a Date and is stored in the MySQL database as YYYY-MM-DD. Select * from re

Why doesn't this date format work?

Earl Grey Tea I'm confused. I read the international specs about the format... but it seems to return nil in playgrounds and code. let dateString = "022018" let formatter = NSDateFormatter() formatter.setLocalizedDateFormatFromTemplate("MMyyyy") let date = f

Date format doesn't work in some computers, SQL query

Shadrak Roulanwa The following date formats don't work on some computers, I do know why, SELECT FORMAT (getdate(), 'dd/MM/yyyy ') as date I use SQL query. What's the question? please can anyone help me Shadrak Roulanwa I am trying to convert the variable inst

Date format doesn't work in some computers, SQL query

Shadrak Roulanwa The following date formats don't work on some computers, I do know why, SELECT FORMAT (getdate(), 'dd/MM/yyyy ') as date I use SQL query. What's the question? please can anyone help me Shadrak Roulanwa I am trying to convert the variable inst

Why doesn't my date work in TypeScript?

Lord Wardmoot I am trying to initialize a Date object in the following way: let date = new Date(2020, 12, 5, 0, 0, 0, 0); then when i do date.getMonth() I get it 0. Can someone help me? and Oswald Months are zero-indexed, so the month to use for initializati

Why doesn't my date work in TypeScript?

Lord Wardmoot I am trying to initialize a Date object in the following way: let date = new Date(2020, 12, 5, 0, 0, 0, 0); then when i do date.getMonth() I get it 0. Can someone help me? and Oswald Months are zero-indexed, so the month to use for initializati

Why doesn't my date comparison work in JavaScript?

ic3man7019 I've researched this problem extensively and found some useful information, but haven't been able to solve my problem. All I want to do is parse one date and compare it to another date. Seems easy, right? Here is what I have tried: function getC

Why doesn't my date work in TypeScript?

Lord Wardmoot I am trying to initialize a Date object in the following way: let date = new Date(2020, 12, 5, 0, 0, 0, 0); then when i do date.getMonth() I get it 0. Can someone help me? and Oswald Months are zero-indexed, so the month to use for initializati

date format doesn't work

Sujwasham I have a month picker in Laravel 4.2. When changing the month, the month and year are displayed as 05/2015. But I need to be in May 2015 format. {{ HTML::style('public/asset/css/smoothness/jquery-ui.css') }} {{ HTML::style('public/asset/css/MonthPick