2

我需要为报告工具中的提示编写一个 sql。我得到一个变量中的多个值的列表,用'-'分隔,这些值的数量可能会有所不同。(例如1.“abc-def”例如2.“abc-def-xyz”)。

现在我需要在这种形式的oracle中编写sql(逻辑上)

select something
  from somewhere
 where someColumn in 'abc' -- use substr to extract abc
    or someColumn in 'def' -- use substr to extract def
    or ...till we don't find '-'.

我不能使用 plsql 进行查询。我可能不知道使用我在 plsql 中选择的变量,或者可能是报告工具不支持该变量。

提前致谢。

4

4 回答 4

6

尝试

select something
  from somewhere
 where someColumn in (select regexp_substr('abc-def-xyz','[^-]+', 1, level) from dual
                     connect by regexp_substr('abc-def-xyz', '[^-]+', 1, level) is not null);

概括(考虑到您的字段用“-”分隔)

select something
  from somewhere
 where someColumn in (select regexp_substr(variable,'[^-]+', 1, level) from dual
                     connect by regexp_substr(variable, '[^-]+', 1, level) is not null);

基本上子查询的输出如下所示 -

  SQL> select regexp_substr('abc-def-xyz','[^-]+', 1, level) value from dual
      connect by regexp_substr('abc-def-xyz', '[^-]+', 1, level) is not null;

VALUE                            
-------------------------------- 
abc                              
def                              
xyz  
于 2012-07-19T03:34:46.717 回答
1

首先使用 Oracle 的 regexp_substr() 函数将字符串拆分为多个部分。请参阅regexp_substr 函数 (如果您可以访问生成字符串的原始函数,请使用它。)

其次,将其放在临时表中,然后只需:

select something
  from somewhere
 where someColumn in (select parts from tmpTable)
于 2012-07-19T03:42:56.480 回答
1
select something
  from somewhere
 where INSTR('-'||'abc-def-ghi'||'-', '-'||someColumn||'-') > 0;
于 2012-07-19T06:18:51.100 回答
0

如果您只想要包含“-”的结果列表,您可以执行类似的操作

SELECT Something From SomeWHERE WHERE Somecolumn LIKE ('%-%');

于 2012-07-19T03:23:00.750 回答