【问题标题】:How to search for texts如何搜索文本
【发布时间】:2019-09-08 09:40:23
【问题描述】:

表格中有一个特定的列,工作人员在其中叙述付款或收据的性质。

写一些类似'PAYMENT OF SALARIES FOR THE MONTH OF NOVEMBER 2018'的东西。我希望能够有代码(ORACLE SQL),可以从这些叙述中挑选月份。

【问题讨论】:

  • 请在您的问题中添加一些代码和数据,以使其完整。

标签: sql oracle


【解决方案1】:

您可以连续使用基于正则表达式的函数 (_replace,_substr,_count) 来提取月份名称,假设所有数据具有相同的格式模型以“月年”组合结尾

with t(str) as
(
 select 'PAYMENT OF SALARIES FOR THE MONTH OF NOVEMBER 2018' from dual
), t2 as
(
select rtrim(str,regexp_replace(str,'(\D)')) as str       
  from t
), t3 as
(  
select regexp_substr(str,'[^ ]+',1,level) as str, 
       regexp_count(str,'[^ ]+') as cnt,
       level as lvl 
  from t2
 connect by level <= regexp_count(str,'[^ ]+')
)
select str 
  from t3 
 where lvl = cnt;

STR
--------
NOVEMBER

【讨论】:

  • 但是如果指定了给定的文本格式,则可以使用 substr(insrt()) 完成操作
  • @nikhilsugandh 谢谢,你说得对,这是另一种方法。
  • @nikhilsugandh 而不是硬编码(8th),最后一行可能更适合使用。
  • Ozhan 都一样,你只是改变了句子中的单词,虽然给了它一个赞成票!!!
  • @nikhilsugandh 顺便说一句,由于单词的数量,您可能会将 9 视为固定值,这会产生 2018
【解决方案2】:

即使您指定的文本格式相同,这也会起作用:

 select * from (select rownum, regexp_substr("column1",'[^ ]+', 1, level) from Table1
 connect by regexp_substr("column1", '[^ ]+', 1, level) is not null) where rownum<=8
 minus
 select * from (select rownum, regexp_substr("column1",'[^ ]+', 1, level) from Table1
 connect by regexp_substr("column1", '[^ ]+', 1, level) is not null) where rownum<8;

http://sqlfiddle.com/#!4/560dc/13

【讨论】:

    【解决方案3】:
     where
      Date >= to_date(substr(:from_date ,1,10),'YYYY/MM/DD') 
      and 
      Date <= to_date(substr(:last_date,1,10),'YYYY/MM/DD')
    

    【讨论】:

      猜你喜欢
      • 2015-06-24
      • 2015-12-31
      • 2019-05-08
      • 1970-01-01
      • 2012-06-28
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多