Skip to content
Advertisement

Oracle SQL: ORA-01858 error on date fields

I have many tables with a date in their name. For example MY_TABLE_2021_06_01, MY_TABLE_2021_06_02, etc. I’m trying to extract the date from the table name and see if any tables are more than an year old. This is my code:

The above code works fine if I don’t include the where clause where table_date < trunc(sysdate)-365. If I run the code with the where clause then I get the

ORA-01858: a non-numeric character was found where a numeric was expected 01858. 00000 – “a non-numeric character was found where a numeric was expected” *Cause: The input data to be converted using a date format model was incorrect. The input data did not contain a number where a number was required by the format model. *Action: Fix the input data or the date format model to make sure the elements match in number and type. Then retry the operation.

I tried a different and long-winded approach to see if I can get my code to work and it still throws the same error. My new code:

Again the above code works fine if I don’t include where old_table = 1 clause but gives the same ORA-01858 error when the where clause is included. I don’t understand this because the field OLD_TABLE isn’t a date field but still getting a date format error.

Advertisement

Answer

Not all tables that satisfy these conditions:

have “valid” date format, or it doesn’t match format mask you used.

Note: if table name is MY_TABLE_2021_06_01, why do you TO_DATE(..., 'DD/MM/YYYY')?

2021_06_01 certainly isn’t in DD/MM/YYYY format, but rather YYYY_MM_DD or YYYY_DD_MM (can’t tell, 06 can be both month or day; the same goes for 01). Maybe your code will start working once you fix that.

If not, then list all tables with names like %_20% and see which one(s) of them violate rules you set.


Example which works for me: sample tables first:

Query to extract tables whose “names” are older than 1 year:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement