![](/img/trans.png)
[英]Oracle SQL Where Condition comparing varchar2 field with number or string
[英]dynamic number of where condition in oracle sql
我需要在報告工具中為提示編寫一個sql。 我得到一個由' - '分隔的變量中的多個值的列表,這些值的數量可以變化。(例如,“abc-def”eg2。“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中使用我選擇的變量,或者報告工具可能不支持。
提前致謝。
嘗試
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
首先使用Oracle的regexp_substr()函數將字符串拆分為其部分。 請參閱regexp_substr函數 (如果您可以訪問生成字符串的原始文件,請使用該函數 。)
其次,將它放在臨時表中,然后只需:
select something
from somewhere
where someColumn in (select parts from tmpTable)
select something
from somewhere
where INSTR('-'||'abc-def-ghi'||'-', '-'||someColumn||'-') > 0;
如果你只想要一個包含' - '的結果列表,你可以做類似的事情
從SOMECOLUMN喜歡的某個地方選擇某些東西('% - %');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.