删除列内的某些值

Posted

技术标签:

【中文标题】删除列内的某些值【英文标题】:Delete certain values inside a column 【发布时间】:2019-11-12 11:25:03 【问题描述】:

我想删除列中的某些值,我有一个列定义,例如:

<listdef page="25" default_order="entity" default_order_type="a"><reportId>1332</reportId><name>test</name><description>test</description><multiProduct>N</multiProduct><product_code>BG,</product_code><equivalentCurrency>EUR</equivalentCurrency><use_absolute_width>Y</use_absolute_width><column name="ref_id"><description locale="fr">Système Id</description></column><column name="iss_date"><description locale="fr">Date d'émission</description></column><column name="exp_date"><description locale="fr">Date de Validité</description></column><column name="bg_amt"><description locale="fr">Montant de la Garantie Bancaire</description></column><column name="applicant_name"><description locale="fr">Nom du donneur d'ordre</description></column><column name="entity"><description locale="fr">Entité</description></column><column name="applicant_reference"><description locale="fr">Référence du donneur d'ordre</description></column><column  name="action"><url action="y"><parameter column="ref_id" name="REFERENCE_ID"/><parameter column="product_code" name="PRODUCT_CODE"/><parameter binding_code="HISTORY" name="OPTION_CODE"/><parameter binding_code="LIST_INQUIRY" name="OPERATION_CODE"/><parameter value="TRADEADMIN_SCREEN" name="SCREEN"/></url></column><column name="bg_liab_amt" hidden="y"/><column name="ref_id" hidden="y"/><column name="product_code" hidden="y"/><column name="ref_id" hidden="y"/><parameter input="y" name="ENTITE" size="" max_length=""><description locale="fr">ENTITE</description></parameter><parameter input="y" name="TIERS" size="" max_length=""><description locale="fr">TIERS</description></parameter><parameter name="export_list" file="stock CBC" type="export" file_name="stock CBC"/><candidate name="BG"><filter><criteria><column name="entity" type="String"/><operator type="like"/><value type="parameter">ENTITE</value></criteria><criteria><column name="applicant_reference" type="String"/><operator type="like"/><value type="parameter">TIERS</value></criteria></filter></candidate><aggregate type="count"><column name="ref_id"/><description locale="fr">Nombre de dossiers</description></aggregate><aggregate type="sum" cur="EUR"><column name="bg_liab_amt"/><description locale="fr">Total encours</description></aggregate><executable_flag>Y</executable_flag></listdef>

我想要的是删除这部分并保留其余部分:

<column  name="action"><url action="y"><parameter column="ref_id" name="REFERENCE_ID"/><parameter column="product_code" name="PRODUCT_CODE"/><parameter binding_code="HISTORY" name="OPTION_CODE"/><parameter binding_code="LIST_INQUIRY" name="OPERATION_CODE"/><parameter value="TRADEADMIN_SCREEN" name="SCREEN"/></url></column>

【问题讨论】:

【参考方案1】:

您可以使用INSTRSUBSTR 函数的组合来实现它,如下所示:

WITH YOUR_TABLE(YOUR_COL) AS
(SELECT
    q'<listdef page="25" default_order="entity" default_order_type="a"><reportId>1332</reportId><name>test</name><description>test</description><multiProduct>N</multiProduct><product_code>BG,</product_code><equivalentCurrency>EUR</equivalentCurrency><use_absolute_width>Y</use_absolute_width><column name="ref_id"><description locale="fr">Système Id</description></column><column name="iss_date"><description locale="fr">Date d'émission</description></column><column name="exp_date"><description locale="fr">Date de Validité</description></column><column name="bg_amt"><description locale="fr">Montant de la Garantie Bancaire</description></column><column name="applicant_name"><description locale="fr">Nom du donneur d'ordre</description></column><column name="entity"><description locale="fr">Entité</description></column><column name="applicant_reference"><description locale="fr">Référence du donneur d'ordre</description></column><column  name="action"><url action="y"><parameter column="ref_id" name="REFERENCE_ID"/><parameter column="product_code" name="PRODUCT_CODE"/><parameter binding_code="HISTORY" name="OPTION_CODE"/><parameter binding_code="LIST_INQUIRY" name="OPERATION_CODE"/><parameter value="TRADEADMIN_SCREEN" name="SCREEN"/></url></column><column name="bg_liab_amt" hidden="y"/><column name="ref_id" hidden="y"/><column name="product_code" hidden="y"/><column name="ref_id" hidden="y"/><parameter input="y" name="ENTITE" size="" max_length=""><description locale="fr">ENTITE</description></parameter><parameter input="y" name="TIERS" size="" max_length=""><description locale="fr">TIERS</description></parameter><parameter name="export_list" file="stock CBC" type="export" file_name="stock CBC"/><candidate name="BG"><filter><criteria><column name="entity" type="String"/><operator type="like"/><value type="parameter">ENTITE</value></criteria><criteria><column name="applicant_reference" type="String"/><operator type="like"/><value type="parameter">TIERS</value></criteria></filter></candidate><aggregate type="count"><column name="ref_id"/><description locale="fr">Nombre de dossiers</description></aggregate><aggregate type="sum" cur="EUR"><column name="bg_liab_amt"/><description locale="fr">Total encours</description></aggregate><executable_flag>Y</executable_flag></listdef>'
FROM DUAL)
SELECT
    SUBSTR(YOUR_COL, 1, STARTPOS) || SUBSTR(YOUR_COL, ENDPOS) AS OUTPUT
FROM (
        SELECT
            YOUR_COL,
            INSTR(YOUR_COL, '<column  name="action">') - 1 AS STARTPOS,
            INSTR(YOUR_COL, 'name="SCREEN"/></url></column>') + 30 AS ENDPOS
        FROM  YOUR_TABLE
    );

Demo

干杯!!

【讨论】:

如果子字符串在输入db<>fiddle中的其他(有效)位置匹配,这将不起作用,如果属性的顺序发生更改,它也会失败。 是的,根据样本数据解决。【参考方案2】:

如果您的值在语法上是有效的 XML/Xhtml,那么您可以将其转换为 XMLTYPE 并使用 DELETEXML 删除 column 标记,其中 name 属性等于 "action"

Oracle 设置

CREATE TABLE test_data ( value ) AS
SELECT q'<listdef page="25" default_order="entity" default_order_type="a"><reportId>1332</reportId><name>test</name><description>test</description><multiProduct>N</multiProduct><product_code>BG,</product_code><equivalentCurrency>EUR</equivalentCurrency><use_absolute_width>Y</use_absolute_width><column name="ref_id"><description locale="fr">Système Id</description></column><column name="iss_date"><description locale="fr">Date d'émission</description></column><column name="exp_date"><description locale="fr">Date de Validité</description></column><column name="bg_amt"><description locale="fr">Montant de la Garantie Bancaire</description></column><column name="applicant_name"><description locale="fr">Nom du donneur d'ordre</description></column><column name="entity"><description locale="fr">Entité</description></column><column name="applicant_reference"><description locale="fr">Référence du donneur d'ordre</description></column><column  name="action"><url action="y"><parameter column="ref_id" name="REFERENCE_ID"/><parameter column="product_code" name="PRODUCT_CODE"/><parameter binding_code="HISTORY" name="OPTION_CODE"/><parameter binding_code="LIST_INQUIRY" name="OPERATION_CODE"/><parameter value="TRADEADMIN_SCREEN" name="SCREEN"/></url></column><column name="bg_liab_amt" hidden="y"/><column name="ref_id" hidden="y"/><column name="product_code" hidden="y"/><column name="ref_id" hidden="y"/><parameter input="y" name="ENTITE" size="" max_length=""><description locale="fr">ENTITE</description></parameter><parameter input="y" name="TIERS" size="" max_length=""><description locale="fr">TIERS</description></parameter><parameter name="export_list" file="stock CBC" type="export" file_name="stock CBC"/><candidate name="BG"><filter><criteria><column name="entity" type="String"/><operator type="like"/><value type="parameter">ENTITE</value></criteria><criteria><column name="applicant_reference" type="String"/><operator type="like"/><value type="parameter">TIERS</value></criteria></filter></candidate><aggregate type="count"><column name="ref_id"/><description locale="fr">Nombre de dossiers</description></aggregate><aggregate type="sum" cur="EUR"><column name="bg_liab_amt"/><description locale="fr">Total encours</description></aggregate><executable_flag>Y</executable_flag></listdef>' FROM DUAL

查询

SELECT DELETEXML( XMLTYPE( value ), '/listdef/column[@name="action"]' ).getStringVal()
FROM   test_data;

输出

| DELETEXML(XMLTYPE(VALUE),'/LISTDEF/COLUMN[@NAME="ACTION"]').GETSTRINGVAL() | | :------------------------------------------------ -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------------------------------------------- | | 1332testtestNBG,EURYSyst??me IdDate d'??mission有效日期??Montant de la Garantie BancaireNom du donneur d'ordreEntit??R??f??rence du donneur d'ordreENTITETIERSENTITETIERSNombre de dossiers总鼓励Y |

db小提琴here

【讨论】:

【参考方案3】:

您可以同时使用regexp_substr()replace() 函数:

select replace( str, 
                regexp_substr(str,'(<column )(.*)(</url></column>)')
              ) as "Extracted String"
  from tab 

其中tab 表示您的原始表,str 表示其中包含共享的长字符串数据的列。

Demo

【讨论】:

这不会删除字符串的所需部分,并按照 OP 的要求保留其余部分。另外,正则表达式可能匹配太多db<>fiddle。 这个查询的结果就是我要删除不要保留的东西 对不起@Blue,我以互补的方式理解了逻辑。现已修复。 它仍然可以删除太多db<>fiddle,并且如果name 属性在width 属性之前也会失败(这对HTML/XML 的含义没有影响)。

以上是关于删除列内的某些值的主要内容,如果未能解决你的问题,请参考以下文章

根据列内的值比较两个数据框

SQL聚合中同一列内的多个值的不同计数

如何在 Eloquent 中查询 JSON 列内的数组

删除指定组内的某些成员

使用sort加个简单的算法,不循环整个数组而删除数组内的某些元素。

数据表列内的 MySQL 循环