YZlat
asked on
Oracle date formatting question
When attempting to insert data into the database i am getting "ORA-01861: literal does not match format string' error.
Here is my insert statement:
INSERT INTO Table1(Field1)VALUES(to_da te('2012-0 6-12 00:00:00.000', 'DD-MON-YYYY'))
can someone please help me insert the date field without changing either '2012-06-12 00:00:00.000' or the default date format in my database?
There must be a way, using to_char or something
Here is my insert statement:
INSERT INTO Table1(Field1)VALUES(to_da
can someone please help me insert the date field without changing either '2012-06-12 00:00:00.000' or the default date format in my database?
There must be a way, using to_char or something
ASKER
it needs to be in DD-MON-YYYY format. Is there a function that can convert '2012-06-12 00:00:00.000' to 12-JUN-2012? I do not need a precision but that's how I get the data
I agree with above comments.
see also this article about how to work correctly with dates and conversion from/to strings:
https://www.experts-exchange.com/Database/Miscellaneous/A_1499-DATE-and-TIME-don%27t-be-scared-and-do-it-right-the-first-time.html
see also this article about how to work correctly with dates and conversion from/to strings:
https://www.experts-exchange.com/Database/Miscellaneous/A_1499-DATE-and-TIME-don%27t-be-scared-and-do-it-right-the-first-time.html
k, if you are using a DATE (datatype) column, the storage is different from the display. What I provided was to convert a text string into the DATE storage.
Show us a short example of the data stream please. I am misunderstanding your VALUES clause.
Show us a short example of the data stream please. I am misunderstanding your VALUES clause.
ASKER
can someone just re-write my insert statement?
as posted above:
INSERT INTO Table1(Field1)VALUES(to_da te('2012-0 6-12 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.SSS'))
or
INSERT INTO Table1(Field1)VALUES(to_da te('12-JUN -2012', 'DD-MON-YYYY'))
INSERT INTO Table1(Field1)VALUES(to_da
or
INSERT INTO Table1(Field1)VALUES(to_da
>>or the default date format in my database?
To clarify what others have posted: Dates in Oracle don't have a format. They are stored in an internal format.
To get a specific format, you convert the internal format into a string at display or from a string to the internal format on insert.
For what you have posted, you are converting a string into a date with TO_DATE. If your input string has a different format, just use the correct format mask to match the data.
>>can someone just re-write my insert statement?
It was in http:#a38820246
If you need something different, please explain more about what you are trying to do.
To clarify what others have posted: Dates in Oracle don't have a format. They are stored in an internal format.
To get a specific format, you convert the internal format into a string at display or from a string to the internal format on insert.
For what you have posted, you are converting a string into a date with TO_DATE. If your input string has a different format, just use the correct format mask to match the data.
>>can someone just re-write my insert statement?
It was in http:#a38820246
If you need something different, please explain more about what you are trying to do.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK, so the dates come from a file in the format:
'2012-06-12 00:00:00.000'
I need to insert it into a date field in the database using INSERT statement. There are A LOT of records, so rewriting each one to '12-JUN-2013' or anything else would be a pain.
What I want is construct my insert in such a way that it automatically converts the date to proper format
'2012-06-12 00:00:00.000'
I need to insert it into a date field in the database using INSERT statement. There are A LOT of records, so rewriting each one to '12-JUN-2013' or anything else would be a pain.
What I want is construct my insert in such a way that it automatically converts the date to proper format
the option has been suggested above already several times, but let me repost it:
INSERT INTO Table1(Field1)VALUES(to_date('2012-06-12 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.SSS'))
Don't do anything to format the date, it won't work.
As mentioned before, dates do NOT have formats.
Only strings have formats.
the main problem here is your string is actually a TIMESTAMP string , not a DATE string.
so, as above, convert your string into a real TIMESTAMP and let oracle downgrade it to a DATE type
insert into yourtable (yourdatecolumn) values (to_timestamp('2012-06-12 00:00:00.000','yyyy-mm-dd hh24:mi:ss.ff'))
As mentioned before, dates do NOT have formats.
Only strings have formats.
the main problem here is your string is actually a TIMESTAMP string , not a DATE string.
so, as above, convert your string into a real TIMESTAMP and let oracle downgrade it to a DATE type
insert into yourtable (yourdatecolumn) values (to_timestamp('2012-06-12 00:00:00.000','yyyy-mm-dd hh24:mi:ss.ff'))
ASKER
sdastuber, that is perfect! Exactly what I wanted. Thanks you!
ASKER
Thank you!
ASKER
AngelIII, you are being very rude. may I point out that this below was NEVER suggested yet.
INSERT INTO Table1(Field1)VALUES(to_da te('2012-0 6-12 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.SSS'))
So please, stop you are not being helpful at all. Thanks God for real experts with manners who are able to provide efficient solutions without trying to insult anyone
INSERT INTO Table1(Field1)VALUES(to_da
So please, stop you are not being helpful at all. Thanks God for real experts with manners who are able to provide efficient solutions without trying to insult anyone
ASKER
and by the way,
INSERT INTO Table1(Field1)VALUES(to_da te('2012-0 6-12 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.SSS'))
gives an error
INSERT INTO Table1(Field1)VALUES(to_da
gives an error
YZlat,
sorry for the typo indeed, it should have been .ff and not .sss ...;
which on top does require to_timestamp, to_date would not work indeed.
I stand corrected for that; I had not tested the code (being on mobile)
but I had posted that suggestion before (https://www.experts-exchange.com/questions/28009193/Oracle-date-formatting-question.html?anchorAnswerId=38820499#a38820499)
I also cannot accept your statement that I would have been rude; and surely not very rude.
anyhow, glad you got your solution!
please don't judge that "single" error on this question on my general database/oracle knowledge, though, which would also be rude.
CHeers
a3
sorry for the typo indeed, it should have been .ff and not .sss ...;
which on top does require to_timestamp, to_date would not work indeed.
I stand corrected for that; I had not tested the code (being on mobile)
but I had posted that suggestion before (https://www.experts-exchange.com/questions/28009193/Oracle-date-formatting-question.html?anchorAnswerId=38820499#a38820499)
I also cannot accept your statement that I would have been rude; and surely not very rude.
anyhow, glad you got your solution!
please don't judge that "single" error on this question on my general database/oracle knowledge, though, which would also be rude.
CHeers
a3
ASKER
I quote
the statement above suggests that an option was suggested several times, which it was not. Saying it was implies I am an idiot who cannot read and ignores solutions posted, meanwhile no solutions were posted and by your own admission you have not even bother testing your solution.
Manners my fiend, manners... Absent indeed...
the option has been suggested above already several times, but let me repost it
the statement above suggests that an option was suggested several times, which it was not. Saying it was implies I am an idiot who cannot read and ignores solutions posted, meanwhile no solutions were posted and by your own admission you have not even bother testing your solution.
Manners my fiend, manners... Absent indeed...
actually angeliii had previously posted that same sql statement, in http:#a38820499
I don't think you should hold it against him that he didn't test it
I didn't test mine either. For short one-liners like this, most people simply post the syntax they think is correct and wait for feedback.
His mistake was basically the same mistake made by others as well. confusing to_date with to_timestamp
yes, it's wrong, but fairly minor and certainly no greater error than others made.
let me assure you angeliii has been answering Oracle questions for longer than I have and with a great deal of accuracy. We all make mistakes, and, having read thousands of his posts I'm sure his intention was not to slight you in anyway.
He may not have helped you on this question; but I hope you'll give him another chance in others.
I don't think you should hold it against him that he didn't test it
I didn't test mine either. For short one-liners like this, most people simply post the syntax they think is correct and wait for feedback.
His mistake was basically the same mistake made by others as well. confusing to_date with to_timestamp
yes, it's wrong, but fairly minor and certainly no greater error than others made.
let me assure you angeliii has been answering Oracle questions for longer than I have and with a great deal of accuracy. We all make mistakes, and, having read thousands of his posts I'm sure his intention was not to slight you in anyway.
He may not have helped you on this question; but I hope you'll give him another chance in others.
For example, accuracy to minutes would be:
INSERT INTO Table1(Field1)VALUES(to_da