7

I understand that Oracle sysdate returns the current date AND time. That's great for timestamp or datetime columns.

Now let's say I have a DATE only column. What keywords should I use on my insert query?

insert into myTable1(myDateOnlyColumn) values(???)

And let's say I have a TIME only column. What keywords should I use on my insert query?

 insert into myTable2(myTimeOnlyColumn) values(???)

Thanks!

4 Answers 4

16

To remove time from sysdate you can just write TRUNC(sysdate).

For example:

SELECT TRUNC(SYSDATE) "TODAY" FROM DUAL;

will give you:

TODAY                     
------------------------- 
'2012-10-02 00:00:00'     
8

There is no such thing as a DATE only column in Oracle. The DATE datatype stores date and time.

If you only care about the date, you can:

INSERT INTO tbl (dtCol) VALUES (TO_DATE('20110929','YYYYMMDD');

This leaves the time component at 00:00:00. You don't have to display it though.

If you're only interested in the time component, you still have a date stored in the column. You'll just have to handle that on output. For example:

SQL> CREATE TABLE dt (d DATE);

SQL> INSERT INTO dt VALUES (TO_DATE('1:164800','J:HH24MISS'));

1 row inserted

Showing the actual contents of the column reveals a date was inserted:

SQL> SELECT * FROM dt;

D
--------------------
0/0/0000 4:48:00 PM

Selecting only the time component from the column gives you the output you want:

SQL> SELECT TO_CHAR(d, 'HH24:MI:SS') d FROM dt;

D
--------
16:48:00

SQL> 

If you think you need only a time column, you'll want to make sure you always insert the same date component.

6
  • So it stores all zeros for the time part when I am storing a birthdate? How about TIME only columns? Sep 29, 2011 at 22:48
  • Good answer. Also, if you have a date that includes hours, minutes and seconds: trunc(sysdate, 'd') to remove them rather than using TO_CHAR
    – WW.
    Sep 29, 2011 at 23:04
  • 1
    @DCookie - Good answer. I'd cover the INTERVAL data types, though, for storing times. If you're only storing a time, you're generally storing a duration (i.e. 4 hours 48 minutes) which maps perfectly to an INTERVAL DAY TO SECOND rather than storing a time like 4:48 pm. Sep 29, 2011 at 23:21
  • @DCookie: Why do you have to pass that 'J' in there? Can I just plug sysdate into these transformations to get the current date and current time? Sep 30, 2011 at 0:29
  • @Sergio, you don't have to, I just used a julian date of 1 to get the year/month/day component to zeroes. Otherwise, you'll wind up with some sort of actual date in there, which, if you're trying to implement a "time only" column might confuse the issue.
    – DCookie
    Sep 30, 2011 at 4:03
0
select sysdate, to_date(to_char(sysdate, 'dd/mm/yyyy'),'dd/mm/yyyy') d
from dual
1
  • 1
    You need to explain why this code will resolve an Op's question already answered?
    – bcesars
    Mar 27, 2015 at 12:53
0

For whoever is looking for a simple solution for returning only date without time, I found this:

to_date(SYSDATE,'dd/mm/yyyy')

Works like a charm. ;-)

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 5, 2021 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.