Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

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_date('2012-06-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
0
YZlat
Asked:
YZlat
  • 8
  • 4
  • 3
  • +2
1 Solution
 
DavidSenior Oracle Database AdministratorCommented:
INSERT INTO Table1(Field1)VALUES(to_date('2012-06-12', YYYY-MM-DD')) -- do you really need the inbound precision down to split seconds?

For example, accuracy to minutes would be:
INSERT INTO Table1(Field1)VALUES(to_date('2012-06-12 00:00', YYYY-MM-DD HH24:MI'))
0
 
YZlatAuthor Commented:
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
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I agree with above comments.
see also this article about how to work correctly with dates and conversion from/to strings:
http://www.experts-exchange.com/Database/Miscellaneous/A_1499-DATE-and-TIME-don%27t-be-scared-and-do-it-right-the-first-time.html
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
DavidSenior Oracle Database AdministratorCommented:
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.
0
 
YZlatAuthor Commented:
can someone just re-write my insert statement?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
as posted above:

INSERT INTO Table1(Field1)VALUES(to_date('2012-06-12 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.SSS'))
or

INSERT INTO Table1(Field1)VALUES(to_date('12-JUN-2012', 'DD-MON-YYYY'))
0
 
slightwv (䄆 Netminder) Commented:
>>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.
0
 
sdstuberCommented:
Are you really trying to insert a string?


INSERT INTO Table1(Field1)VALUES (to_char(timestamp '2012-06-12 00:00:00.000','DD-MON-YYYY'))


if you really want a date out of that timestamp string then, simply insert and the timestamp will downgrade to a DATE type fine


INSERT INTO Table1(Field1)VALUES (timestamp '2012-06-12 00:00:00.000);

or

INSERT INTO Table1(Field1)VALUES (to_timestamp('2012-06-12 00:00:00.000','yyyy-mm-dd hh24:mi:ss.ff');
0
 
YZlatAuthor Commented:
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
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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'))

Open in new window

0
 
sdstuberCommented:
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'))
0
 
YZlatAuthor Commented:
sdastuber, that is perfect! Exactly what I wanted. Thanks you!
0
 
YZlatAuthor Commented:
Thank you!
0
 
YZlatAuthor Commented:
AngelIII, you are being very rude. may I point out that this below was NEVER suggested yet.

INSERT INTO Table1(Field1)VALUES(to_date('2012-06-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
0
 
YZlatAuthor Commented:
and by the way,

INSERT INTO Table1(Field1)VALUES(to_date('2012-06-12 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.SSS'))

gives an error
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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 (http://www.experts-exchange.com/Database/Oracle/Q_28009193.html#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
0
 
YZlatAuthor Commented:
I quote

the option has been suggested above already several times, but let me repost it

Open in new window


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...
0
 
sdstuberCommented:
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.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 8
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now