SQL 如何只存储时间;不是日期和时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1786418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:31:39  来源:igfitidea点击:

How to store only time; not date and time?

sqloracletime

提问by fvital

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

在一个字段中,我不需要存储日期时间对,即标准 Oracle 日期。

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.

我认为可以节省磁盘空间(我有 200 万行)或提供更快的处理。

采纳答案by David Aldridge

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.

您可以尝试使用 INTERVAL DAY TO SECOND 数据类型,但它不会为您节省任何磁盘空间……不过它非常适合此目的。

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.

虽然是 11 个字节。

回答by kurosch

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

您最好的选择可能是将“自午夜以来的秒数”存储为数字字段。

SELECT to_char( SYSDATE, 'SSSSS' ) FROM dual;

回答by Andy West

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.

但是没有可以帮助您节省空间的仅限时间的数据类型。

回答by Marius Burz

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

您将节省几 Mb 的磁盘空间(这在当今已经算不了什么),而且性能几乎一无所获。

You could use a column of NUMBERtype 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 DATEcolumn)

您可以NUMBER按照建议使用类型列来存储自午夜以来的秒数,只是不要忘记约束。
(您可能会NUMBER(5, 0)根据存储的值使用which 使用 1-3 个字节,而不是DATE列使用的常量 7 个字节)

回答by viduka

you can use:

您可以使用:

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

example

例子

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

for time format you can check in here

对于时间格式,您可以在此处登记