0

Sorry for the noob question (I'm trying to practice SQL with some databases), but how can I convert the following Excel date formats to MM-DD-YYYY?

The original Excel column is in American format 'MM-DD-YYYY' and it goes to SQL as VARCHAR2:

enter image description here

So, as far as I know, I need to convert it to a number and then use 'TO_DATE()' function (just got the first 50 rows to test):

     SELECT TO_DATE(TO_NUMBER(DATE1), 'MM-DD-YYYY')  FROM (
         SELECT * FROM CHEMICALS2 FETCH FIRST 50 ROWS ONLY
     )

and got the error '01843.0000 - Not a valid month'

enter image description here

I also tried to add 41293, after converting to number but got the same error.

Do you have a better idea to format that instead of changing the original csv file?

1
  • Stop. Alter your table. Change the column data type to DATE. Commented Oct 24, 2018 at 10:18

1 Answer 1

1

Try removing TO_NUMBER function.

An example,

SELECT TO_DATE(DATE1, 'MM-DD-YYYY')  FROM (
    SELECT '10-18-2018' as date1 from dual
);

--UPDATE --Important note: 37894 it's the number from the excel cell

select (to_date('01011900','DDMMYYYY')-2)+37894 from dual;

Please: check if returns the correct date.

Good luck

Sign up to request clarification or add additional context in comments.

3 Comments

got the same ORA-01843: not a valid month error. The dates to be transformed are in Excel's format, such as 43070, 43344, 43282, but even applying some transforming functions (in other forums), I got the same problem...
mmm... Ok, according with the next link community.oracle.com/thread/205184?start=0&tstart=0 the excel date number is an internal representation. I've modified my answer to put their recommendation. Please, let me know if it works fine
Got it! Thank you very much! I put the following: SELECT (to_date('01011900','DDMMYYYY')-1) + A.DATE1 AS "DATE2" FROM ( SELECT DISTINCT DATE1 FROM CHEMICALS2 FETCH FIRST 50 ROWS ONLY ) A

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.