Avatar of brgdotnet
brgdotnet
Flag for United States of America asked on

How to get the current date and Time upon oracle insert into a database table

How can I get the current date AND time ? I have experimented, but no success, because I am only getting the date and not the time.
Can someone help me out?

(CURRENT_TIMESTAMP and or TO_DATE(sysdate,'dd/mm/yyyy hh:mi:ss')   )

This is what I tried :

INSERT INTO MYTABLE (TABLENAME, DATETIME) VALUES ('Test',CURRENT_TIMESTAMP)

INSERT INTO MYTABLE (TABLENAME, DATETIME) VALUES ('Test',TO_DATE(sysdate,'dd/mm/yyyy hh:mi:ss'))
Oracle Database

Avatar of undefined
Last Comment
johnsone

8/22/2022 - Mon
johnsone

Do no put a TO_DATE function on SYSDATE.  It is already a date, there is no need to covert it.  In fact, what is happening is this:

TO_DATE(TO_CHAR(SYSDATE))

Based on your default NLS_DATE_FORMAT you are probably losing the time in that conversion.

If the data type of the column in the database is TIMESTAMP, then use SYSTIMESTAMP.

If the data type of the column in the table is DATE, then use SYSDATE.
ASKER CERTIFIED SOLUTION
slightwv (䄆 Netminder)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
brgdotnet

ASKER
Based on your reply, I tried this, but it is not working? I am still getting only the date and not the time.

INSERT INTO MYTABLE (TABLENAME, DATETIME) VALUES ('Test',sysdate);
slightwv (䄆 Netminder)

>>I am still getting only the date and not the time.

Just in case you missed my post:
How do you know?  When you select it, are you providing the correct format mask?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
brgdotnet

ASKER
What do you mean sir? After I run the insert code, I then select everything from the table, and  I get the following : 09-MAR-17

SELECT * FROM MYTABLE

gives :

'Test', 09-MAR-17

Should my select be different, or should my insert sql statement be different? Thank you so much for working through this. I am just trying to understand.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.