Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store only time; not date and time?

Tags:

sql

time

oracle

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.

like image 719
fvital Avatar asked Nov 23 '09 22:11

fvital


People also ask

How can store time without date in SQL?

If you only want to know to the minute, you can store it as an int in the range of 1-1440 . 1 is 00:01 and 1440 is 0:00 . An additional advantage of this is that if you use a smallint data type you are saving 1-3 bytes per record from the built-in TIME datatype.

How can I get just time from a date?

Extract time only from datetime with formula 1. Select a blank cell, and type this formula =TIME(HOUR(A1),MINUTE(A1), SECOND(A1)) (A1 is the first cell of the list you want to extract time from), press Enter button and drag the fill handle to fill range.

What is the data type to store time in Oracle?

The TIMESTAMP datatype is an extension of the DATE datatype. It stores year, month, day, hour, minute, and second values. It also stores fractional seconds, which are not stored by the DATE datatype.


2 Answers

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.

like image 87
David Aldridge Avatar answered Sep 20 '22 16:09

David Aldridge


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

SELECT to_char( SYSDATE, 'SSSSS' ) FROM dual; 
like image 29
kurosch Avatar answered Sep 20 '22 16:09

kurosch