简体   繁体   English

Oracle中的时间戳数据类型查询

[英]Timestamp data type query in Oracle

I have a table called CYCLING_ACCIDENTS_2 containing a TIMESTAMP(6) column called ACC_DATE_TIME , this is an example of how the date is stored 31-MAY-12 16.45.00.000000 , I would like to know how I can query just the time in such a date format so that I can have a time interval for all years I have (2005-2012) but just restricted to certain times in the day. 我有一个称为表CYCLING_ACCIDENTS_2含有TIMESTAMP(6)称为列ACC_DATE_TIME ,这是日期是如何被存储的例子31-MAY-12 16.45.00.000000 ,我想知道如何可以在这样的查询只是时间日期格式,这样我就可以为所有年份(2005-2012)设置一个时间间隔,但仅限于一天中的某些时间。 I tried many functions but all I've got so far are syntax errors, I tried to search on the web but I can' t see anything appropriate to my case. 我尝试了许多功能,但到目前为止,我所得到的只是语法错误,我尝试在网络上搜索,但找不到适合我情况的任何内容。 Could anyone help? 有人可以帮忙吗?

Thanks! 谢谢!

First of all, a timestamp is a number, not a string. 首先,时间戳是数字,而不是字符串。 So the date is displayed by default as 31-MAY-12 16.45.00.000000 , but it is actually the amount of microseconds since 1970 I believe. 因此,日期默认情况下显示为31-MAY-12 16.45.00.000000 ,但我相信它实际上是自1970年以来的微秒数。

If you want to select just the time part use to_char() 如果只想选择时间部分,请使用to_char()

select to_char(acc_date_time, 'hh24:mi') time
,      count(*) occurences
from   cycling_accidents_2
group by to_char(acc_date_time, 'hh24:mi')

edit : I think this second query actually answers your question: 编辑 :我认为这第二个查询实际上回答您的问题:

select *
from   cycling_accidents_2 ca
where  to_char(ca.acc_date_time, 'hh24:mi') between '10:00' and '18:00'
and    ca.acc_date_time >= to_timestamp('01-01-2005', 'dd-mm-yyyy') 
and    ca.acc_date_time < to_timestamp('01-01-2013', 'dd-mm-yyyy')
SELECT * FROM CYCLING_ACCIDENTS_2 WHERE
(EXTRACT(YEAR FROM ACC_DATE_TIME) BETWEEN 2005 AND 2012)
AND
(EXTRACT(HOUR FROM ACC_DATE_TIME) BETWEEN 10 AND 18)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM