31

In one field I need to store not a datetime pair, i.e. a standard Oracle date.

01/10/2009 22:10:39

But time only

22:10:39

I think that save disk space (I have 2 million rows) or provide faster processing.

3
  • 3
    In the grand scheme of things, 2 million rows is practically nothing. Is the meager space saving worth the loss in precision?
    – kurosch
    Nov 23, 2009 at 22:28
  • 2
    I guess the question I should have asked is "faster processing of what?" If all you have it time-of-day, you can't compare it to anything else reliably, so you could just not store anything and save yourself the whole 7 bytes per row.
    – kurosch
    Nov 23, 2009 at 22:42
  • Another application area where time-of-day should only be stored but we don't care about a date: repeating event starting times. E.g. an event's start and end times are stored in MTG_START and MTG_END. But these apply to different days/dates, depending on the week. I want to only store the times of day so I don't confuse future developers/users of the database table. Is there a way to do this?
    – Kalin
    Mar 5, 2020 at 17:01

5 Answers 5

22

You could try the INTERVAL DAY TO SECOND data type but it won't save you any disk space ... it is very suitable for this purpose though.

create table t1 (time_of_day interval day (0) to second(0));

insert into t1 values (TO_DSINTERVAL('0 23:59:59'));

select date '2009-05-13'+time_of_day
from   t1;

11 bytes though.

6

Your best bet would probably be storing "seconds since midnight" as a number field.

SELECT to_char( SYSDATE, 'SSSSS' ) FROM dual;
3
  • 1
    Of course, you must factor in (1) perofrmance penalty for always converting values and (2) developement time penalty for using weird calculations for otherwise standard date arithmetics.
    – jva
    Nov 24, 2009 at 13:59
  • @jva: yeah, I was going to mention that but by the time I thought of it I figured this thread was pretty much dead so why bother
    – kurosch
    Nov 24, 2009 at 14:31
  • 1
    I do like this approach but it's worth mentioning two things. The data should be stored in a number to ensure that you can't insert characters and there should be a check constraint on the column ensuring that you can't input more than 24 hours in seconds in.
    – Ben
    Feb 23, 2013 at 21:20
3

You can extract the time from a date as a string like this:

to_char(sysdate,'HH.MI.SS')

but there is no time-only data type that will help you save space.

1
  • If you store it as a VARCHAR anything can be stored in the column, which isn't very useful when trying to get it out again.
    – Ben
    Feb 23, 2013 at 21:19
1

you can use:

TO_CHAR(<DATE_COLUMN>, '<TIME_FORMAT>');

example

TO_CHAR(SYSDATE, 'HH24:MI:SS');

for time format you can check in here

0

You would save a few Mb of disk space(which is nothing nowadays) and you would gain next to nothing in performance.

You could use a column of NUMBER type for storing the number of seconds since midnight as suggested, just don't forget about the constraints.
(You'd probably use NUMBER(5, 0) which uses 1-3 bytes depending on the stored value, instead of a constant 7 bytes used by a DATE column)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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