how to insert date and time in oracle?

Oracle

Oracle Problem Overview


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...

Oracle Solutions


Solution 1 - Oracle

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.

Solution 2 - Oracle

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.

Solution 3 - Oracle

Try this:

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

Solution 4 - Oracle

Just use TO_DATE() function to convert string to DATE.

For Example:

create table Customer(
       CustId int primary key,
       CustName varchar(20),
       DOB date);

insert into Customer values(1,'Vishnu', TO_DATE('1994/12/16 12:00:00', 'yyyy/mm/dd hh:mi:ss'));

Solution 5 - Oracle

create table Customer(
       CustId int primary key,
       CustName varchar(20),
       DOB date);

insert into Customer values(1,'kingle', TO_DATE('1994-12-16 12:00:00', 'yyyy-MM-dd hh:mi:ss'));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeepView Question on Stackoverflow
Solution 1 - OracleSai prateekView Answer on Stackoverflow
Solution 2 - OracleRob van WijkView Answer on Stackoverflow
Solution 3 - OraclejschorrView Answer on Stackoverflow
Solution 4 - OracleCodemakerView Answer on Stackoverflow
Solution 5 - OraclekingleView Answer on Stackoverflow