1

I have a table in which there is a column with datatype TIMESTAMP(0)

When I insert a date into this column using

INSERT INTO TEST_TIMESTAMP VALUES(SYSDATE)

it inserts a date in the following example format

12-SEP-12 10.31.19.000000000 AM

I want to know how the below timestamp formats can be inserted in the table

12-SEP-12 10.31.19 and 12-SEP-12 10.31.19 AM

I tried specifying some formats using TO_CHAR while inserting SYSDATE into the table, but it didn't work.

Please suggest.

2
  • 2
    A timestamp or date datatype doesn't have a format. A format is only needed when you retrieve the value to display it to a user. Commented Sep 12, 2012 at 14:57
  • TIMESTAMP(0) is effectively the same as DATE, which will store times down to the second anyway. Your NLS_DATE_FORMAT setting may not make that obvious as it could only show the date part by default, but TO_CHAR(<date_field>, 'DD-Mon-RR HH24.MI.SS') would include the time. (The data types are explained in the documentation) Commented Sep 12, 2012 at 15:02

4 Answers 4

3

when you store a TIMESTAMP it will always store the data at maximum precision (with fractional seconds).

I think what you want to do is supply a format to display the date when you retrieve it from the database.

You can do this like so:

select to_char(timestampColumnName,'DD-MON-YY HH24:MI:SS') "Date" from test_timestamp

or

select to_char(timestampColumnName,'DD-MON-YY HH:MI:SS AM') "Date" from test_timestamp
Sign up to request clarification or add additional context in comments.

2 Comments

@paul- i know how to retrieve the timestamp in different formats. Only concern is how can i insert it in the specified format as mentioned above in the problem?
are you wanting to use something like TO_TIMESTAMP('12-SEP-12 10.31.19','DD-MON-YY HH.MI.SS')?
1

You can return it very easy like:

SELECT to_char(sysdate,'DD-MON-YY HH24:MI:SS') FROM DUAL;

In your case use:

INSERT INTO TEST_TIMESTAMP(column_name) VALUES(to_char(sysdate,'DD-MON-YY HH24:MI:SS'));

Comments

0

You were missing the extra ().

INSERT INTO TEST_TIMESTAMP
VALUES (TO_TIMESTAMP('12-SEP-12 10.31.19', 'DD-MON-YY HH.MI.SS'));

INSERT INTO TEST_TIMESTAMP
VALUES (TO_TIMESTAMP('12-SEP-12 10.31.19 AM', 'DD-MON-YY HH.MI.SS AM'));

Comments

-1
TO_CHAR(SYSDATE, 'DD/MM/YYYY HH24:MI:SS')

This for colum type insert over mode to_char.

Comments

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.