convert varchar to date oracle plsql

Asked

Viewed 1,396 times

0

I need to convert a varchar for date so that you can filter all dates longer than a certain date, for example:

I want all dates that are larger than November 2018 to be listed, I am doing as follows:

DATA_FINAL > TO_DATE('10/11/2018', 'MM/DD/YYYY')

But I am always returned the error:

ORA-01843: not a Valid Month

What should I do? I thank you in advance.

Result of SUBSTR:

    1   09
    2   08
    3   04
    4   26
    5   28
    6   07
    7   17
    8   13
    9   11
    10  12
    11  01
    12  18
    13  21
    14  16
    15  31
    16  20
    17  22
    18  29
    19  19
    20  25
    21  02
    22  05
    23  03
    24  10
    25  15
    26  14
    27  27
    28  06
    29  24
    30  30
    31  23
  • 1

    Run: select TO_DATE('10/11/2018', 'MM/DD/YYYY') from dual also occurs the error? If it does not occur (as it did not happen with me) the field DATA_FINAL is filled in any record with invalid date.

  • I made this select here and it worked smoothly. Is there anything saved in that DATA_FINAL field that is causing the error.

  • select TO_DATE('10/11/2018', 'MM/DD/YYYY') from dual works, but in select da the error I mentioned, and so I saw no date with wrong format.

  • Select only a few lines. I think it will work. Until you select the line that DATA_FINAL is invalid.

  • If your dates are in American format your to_date is 100% correct! A possible test would be to replace to_date with a Sysdate just to check if the error persists, eliminating the date formatting and etc. Another detail, the Data_final field is of the correct date type?

  • My date is in pt-br format but of the 2 forms happens the error

  • Confuse, the field DATA_FINAL is of type VARCHAR, I am converting to date

  • DATA_FINAL > Sysdate Error message tb

  • Select this and post the result: select distinct substr( data_final, 1, 2 ) from your table

  • Reginaldo Rigo, I posted the result on top.

  • From what I understand the first column is the row and the second the result of the substr. Right? If that’s right. all numbers above 12 are invalid because you are trying to convert them to month. Hence the message: NOT A VALID MONTH.

  • Reginaldo Rigo, but I switched from mm/dd/yyyy to dd/mm/yyyy

  • If it still went wrong, it’s because you’re still an invalid. Check if the content of select distinct substr( data_final, 3 ,2 ) comes something other than 1 to 12

  • @Standalone, but in this way you are comparing a string with a date, the correct one would be to convert the varchar field to date as well. If it is not too laborious, it is advisable you convert this column to date type, record date in string format is open a port to a thousand different problems ...

Show 9 more comments

1 answer

1

--this block lists dates that are not in expected format

set serveroutput on;--se sql plus, senao setar o output na ferramenta

declare
  vd date;
begin
  for r in (select data_final from tabela)
    loop
      begin
        vd := to_date(r.data_final,'mm/dd/yyyy');
      exception
        when others then
          dbms_output.put_line('data com erro' || r.data_final);
      end;
    end loop;
end;

At least you’ll have what’s causing the problem

  • the result was number 14... not understood...

  • 14 is not a valid date in the expected format.

  • No, I put in the condition DATA_FINAL = '14' and find nothing.

  • Data Like '%14' and length(Trim(date)) < 3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.