Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to insert date and time in oracle?

Tags:

oracle

Im having trouble inserting a row in my table. Here is the insert statement and table creation. This is part of a uni assignment hence the simplicity, what am i doing wrong? Im using oracle SQL developer Version 3.0.04.'

The problem i am having is that it is only inserting the dd/mon/yy but not the time. How do i get it to insert the time as well?

INSERT INTO WORKON (STAFFNO,CAMPAIGNTITLE,DATETIME,HOURS)
VALUES ('102','Machanic Summer Savings',TO_DATE('22/April/2011 8:30:00AM','DD/MON/YY HH:MI:SSAM'),'3')
;

CREATE TABLE WorkOn
(
    StaffNo        NCHAR(4),
    CampaignTitle  VARCHAR(50),
    DateTime       DATE,
    Hours          VARCHAR(2)
)
;

Thanks for the help.

EDIT: This is making no sense, i enter just a time in the field to test if time is working and it outputs a date WTF? This is really weird i may not use a date field and just enter the time in, i realise this will result in issues manipulating the data but this is making no sense...

like image 686
Deep Avatar asked Sep 24 '11 02:09

Deep


People also ask

How do I insert date in Oracle?

SELECT customer_id, TO_CHAR(dob, 'MONTH DD, YYYY') FROM customers; The next query gets the current date and time from the database using the SYSDATE function, then converts the date and time to a string using TO_CHAR() with the format MONTH DD, YYYY, HH24:MI:SS.

What is the datatype for date and time in Oracle?

Datetime and Interval Datatypes. The datetime datatypes are DATE , TIMESTAMP , TIMESTAMP WITH TIME ZONE, and TIMESTAMP WITH LOCAL TIME ZONE . Values of datetime datatypes are sometimes called datetimes. The interval datatypes are INTERVAL YEAR TO MONTH and INTERVAL DAY TO SECOND .

How do I add a TIMESTAMP in Oracle?

You can use the below code: insert into tablename (timestamp_value) values (TO_TIMESTAMP(:ts_val, 'YYYY-MM-DD HH24:MI:SS')); If you need the current timestamp to be inserted then use the following code: insert into tablename (timestamp_value) values (CURRENT_TIMESTAMP);


3 Answers

You can use

insert into table_name
(date_field)
values
(TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'));

Hope it helps.

like image 176
Sai prateek Avatar answered Oct 16 '22 22:10

Sai prateek


You are doing everything right by using a to_date function and specifying the time. The time is there in the database. The trouble is just that when you select a column of DATE datatype from the database, the default format mask doesn't show the time. If you issue a

alter session set nls_date_format = 'dd/MON/yyyy hh24:mi:ss'

or something similar including a time component, you will see that the time successfully made it into the database.

like image 33
Rob van Wijk Avatar answered Oct 16 '22 22:10

Rob van Wijk


Try this:

...(to_date('2011/04/22 08:30:00', 'yyyy/mm/dd hh24:mi:ss'));

like image 9
jschorr Avatar answered Oct 17 '22 00:10

jschorr