簡體   English   中英

將 CLOB 列數據轉換為 NUMBER 字段

[英]Convert CLOB column data translated to NUMBER field

我正在使用 ORACLE SQL 開發人員。 我有一個表 Clob_tab,其數據類型為: Col1 CLOB data in this column is ('27,88,100,25,26')

我想將此列中的該值用作另一個查詢的子查詢: select * from Abc where Col2 not in (select Col1 from Clob_tab ); Col2 的 DATATYPE 是 NUMBER。 我希望 output 為:select * 來自 Abc,其中 Col2 不在 (27,88,100,25,26) 中;

可能嗎?

我嘗試了多種不起作用的方法,例如:將 blob 轉換為 varchar2:dbms_lob.substr(MY_FIELD_BLOB_TYPE) 使用 regex_replace 將 varchar2 轉換為數字:select cast(regexp_replace(Col1, '[^0-9]+', '') as編號)來自 Clob_tab。 使用 regex_repalce 所有逗號都消失了,我得到 27881002526。我不想要這個數字。 我想要用逗號分隔的數字。

他們都沒有將我的查詢提供/翻譯成這種形式:

select * 來自 Abc,其中 Col2 不在 (27,88,100,25,26) 中;

我找到了解決這個問題的簡單方法:)

select regexp_substr(OBJECT_NAME,'[^,]+', 1, level) from prachi_exp_cycle connect by regexp_substr(OBJECT_NAME, '[^,]+', 1, level) 不是 null;

它將返回 output 作為逗號分隔的字符值 25、26、27。 當用作子查詢時,這些輸出將在 SQL 開發人員中自動轉換為數字。

有多種方法可以標記您的字符串; 這使用正則表達式:

with cte (n) as (
  select to_number(regexp_substr(col1, '(.*?)(,|$)', 1, level, null, 1))
  from clob_tab
  connect by level < regexp_count(col1, '(.*?)(,|$)')
)
select *
from abc
where col2 not in (
  select n from cte
);

這使用 XMLTable 將值視為序列並提取它們:

select *
from abc
where col2 not in (
  select to_number(x.column_value)
  from clob_tab
  cross join xmltable (col1) x
);

根據您的 Oracle 版本,您可能可以使用 JSON 而不是 XML 執行類似的操作:

select *
from abc
where col2 not in (
  select x.n
  from clob_tab
  cross join json_table (json_array(col1 format json), '$[*]' columns n number path '$') x
);

db<>小提琴

值得嘗試各種方法並將性能與您的數據進行比較。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM