[英]How to find time of day or range in TIMESTAMP column
I have a list of events with begin and end timestamps in GMT UTC+0. 我在GMT UTC + 0中列出了带有开始和结束时间戳记的事件。 The column is type DATE but has time as well (not designed by me).
该列为DATE类型,但也有时间(不是我设计的)。
The begin timestamp is plainly indexed 开始时间戳已明确索引
I need to find events the occur between, say, 06:00 and 22:00 Localtime which is Eastern Daylight Time UTC-4. 我需要查找发生在当地时间06:00和22:00之间的事件,这是美国东部夏令时间UTC-4。 on any day between March 31th and April 2nd.
在3月31日至4月2日之间的任何一天。
The Only way I've found to do it is convert it TO_CHAR() then. 我发现做到这一点的唯一方法就是将其转换为TO_CHAR()。 It's also not using the index because it's using TO_CHAR function.
它也没有使用索引,因为它使用的是TO_CHAR函数。
Here's what I go so far. 这是我到目前为止的事情。
TO_CHAR(e.begin_time,'HH24:MI') >= TO_CHAR(FROM_TZ(TO_TIMESTAMP('06:00','HH24:MI'),'US/Eastern') AT TIME ZONE '+00:00','HH24:MI') AND
TO_CHAR(e.begin_time,'HH24:MI') <= TO_CHAR(FROM_TZ(TO_TIMESTAMP('22:00','HH24:MI'),'US/Eastern') AT TIME ZONE '+00:00','HH24:MI') AND
TO_CHAR(e.begin_time,'DDMMYYYY HH24:MI') >= TO_CHAR(FROM_TZ(TO_TIMESTAMP('Mar-31-2012 00:00','MON-DD-YYYY HH24:MI'),'US/Eastern') AT TIME ZONE '+00:00','DDMMYYYY HH24:MI') AND
TO_CHAR(e.begin_time,'DDMMYYYY HH24:MI') <= TO_CHAR(FROM_TZ(TO_TIMESTAMP('Apr-2-2012 23:59','MON-DD-YYYY HH24:MI'),'US/Eastern') AT TIME ZONE '+00:00','DDMMYYYY HH24:MI')
Thanks in advance, 提前致谢,
Dan 担
The performance of the following might not be what's wanted, but if you cast the DATE value to a TIMESTAMP you can use the EXTRACT function in a manner similar to: 以下各项的性能可能不是所需要的,但是如果将DATE值强制转换为TIMESTAMP,则可以使用类似于以下方式的EXTRACT函数:
SELECT *
FROM your_table e
WHERE e.BEGIN_TIME BETWEEN TO_DATE('31-MAR-2012 00:00:00',
'DD-MON-YYYY HH24:MI:SS')
AND TO_DATE('02-APR-2012 23:59:59',
'DD-MON-YYYY HH24:MI:SS') AND
EXTRACT(HOUR FROM CAST(e.BEGIN_TIME AS TIMESTAMP)) BETWEEN 6 AND 22
Share and enjoy. 分享并享受。
When trying to grab data based on a portion of a date field, of course the index will not be used. 当尝试基于日期字段的一部分获取数据时,当然不会使用索引。 there just ain't any workaround to that.
那里没有任何解决方法。 You could look into adding a function-based index, however, to index on the minutes of the day in EST.
但是,您可以考虑添加基于函数的索引,以便在EST中的一天中的分钟数建立索引。 Or, perhaps, add a new column to hold minutes_est, have it populated by an insert/update trigger and then base your query on that.
或者,也许添加一个新列来保存minutes_est,让它由插入/更新触发器填充,然后基于此查询。
If this is a regularly used query, and the performance of a full table scan isn't cutting it, then yes - I think this is one of those times where you might need to do some redesign based on your needs for this. 如果这是一个经常使用的查询,并且不能进行全表扫描,那么可以-我认为这是您可能需要根据自己的需要进行一些重新设计的时候之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.