2012-08-31 52 views
0

我在这里遇到问题。SQL:如何根据列的部分字符串来选择列

我想选择一个基于该列的最大(时间戳)的列,但我坚持检索它。比方说,我有以下数据:

Comments 
======== 
abcd 2012/08/14 8:03:03 AM more data inside <- I want to retrieve this 
hshsh 2012/08/13 1:03:03 AM some other comments 
hahhah 2012/08/10 8:03:03 PM test it. 

我的SQL检索最大(时间戳)是

select max(substr(comments, instr(comments, '2012'), instr(comments, 'M') - 6)) from TABLEA 

但我如何选择此基础上声明此列?

编辑:

是否有可能做到这一点:

select comments from tableA where comments like (select max(substr(comments, instr(comments, '2012'), instr(comments, 'M') - 6)) from TABLEA) 

我希望得到的评论栏的第一行,但没有输出。 我希望我不太模糊......我正在使用Oracle SQL。

回答

0

你可以试试:

select 
    comments, substr(comments, instr(comments, '2012'), instr(comments, 'M') - 6) YEAR 
    from TABLEA 
    order by substr(comments, instr(comments, '2012'), instr(comments, 'M') - 6) desc 
    where rownum = 1 
+0

感谢Alfasin,这是相当接近,但我想要得到的意见,而不是最大(年)来代替,不知道这是可能的。 :-) –

+0

我现在无法测试 - 但我相信这应该起作用。查看更新的答案。 – alfasin

+0

非常接近,谢谢! :) –

0

尝试:

select * 
from 
(
    select comments 
    from TABLEA 
    order by substr(comments, instr(comments, '2012'), instr(comments, 'M') - 6) desc 
) 
where rownum = 1