[英]SQL Oracle datetime string format filter
In oracle, I have table called Ship as shown below; 在oracle中,我有一个名为Ship的表,如下所示;
The date and time are present in a single column as string format. 日期和时间以字符串格式显示在单个列中。
f_ID f_type f_date
1001 A 3/30/14 12:00:44 PM
1001 B 3/30/14 10:05:45 AM
1002 A 2/4/14 11:31:11 AM
1002 B 2/3/14 9:43:21 AM
1003 A 2/3/14 9:37:04 AM
1003 B 12/13/14 10:13:43 AM
1004 A 2/3/14 10:49:50 AM
1005 B 2/3/14 11:00:43 AM
1006 A 2/3/13 11:02:28 AM
1006 B 2/3/14 11:07:23 AM
1006 C 2/3/14 9:19:11 AM
1007 A 2/4/14 11:32:30 AM
1007 B 2/4/14 11:32:30 AM
1007 C 2/4/14 11:32:30 AM
1007 D 2/4/14 11:32:30 AM
I wish to query/list IDs ( f_ID
) which only have exact date for each record, but the time stamps can be different. 我希望查询/列出每个记录仅具有确切日期的ID( f_ID
),但时间戳可以不同。
How can I query this in Oracle? 如何在Oracle中查询? I'm having difficulty in querying as the date and time are merged in a single column. 由于日期和时间合并在单个列中,因此查询时遇到困难。
The result should be 结果应该是
f_ID f_type f_date
1001 A 3/30/14
1001 B 3/30/14
1004 A 2/3/14
1005 B 2/3/14
1007 A 2/4/14
1007 B 2/4/14
1007 C 2/4/14
1007 D 2/4/14
To get the list of s_id
and dates, use an aggregation with a having
clause: 要获取列表s_id
和日期,使用与聚合having
条款:
select s.s_id, trunc(date), count(*)
from ship s
group by s.s_id, trunc(date)
having min(date) <> max(date)
EDIT: 编辑:
If your date is a string, then you should fix the data. 如果您的日期是一个字符串,那么您应该修复数据。 The format that you have is awful. 您的格式太糟糕了。 If you have to store a date as a string, always use the ISO standard YYYY-MM-DD HH:MI:SS format (or something similar). 如果必须将日期存储为字符串,请始终使用ISO标准YYYY-MM-DD HH:MI:SS格式(或类似格式)。 But you can still do what you want: 但是您仍然可以做您想做的事情:
select s.s_id, substr(date, 1, instr(date, ' ')), count(*)
from ship s
group by s.s_id, substr(date, 1, instr(date, ' '))
having min(date) <> max(date)
I would do: 我会做:
select f_ID, f_type, trunc(f_date) f_date
from Ship
order by f_date;
This will allow you to detect the lines with same date, whatever the timestamp. 无论时间戳如何,这将使您能够检测具有相同日期的行。
You can ignore the time using the TRUNC
function: 您可以使用TRUNC
函数忽略时间:
SELECT fdid
FROM ship
WHERE TRUNC(ship.date_field) = SOME_DATE;
If you just want to get all the possible dates: 如果您只想获取所有可能的日期:
SELECT DISTINCT(TRUNC(date_field))
FROM ship;
This should do what you need: 这应该可以满足您的需求:
Select f_id, f_type, SubStr(f_date, 1, InStr(f_date, ' '))
From Ship s
Where Exists( Select 1
From Ship x
Where SubStr(s.f_date, 1, InStr(s.f_date, ' ')) =
SubStr(x.f_date, 1, InStr(x.f_date, ' '))
And s.f_date <> x.f_date )
Order By f_id, f_type
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.