简体   繁体   English

Oracle 将分隔的字符串拆分成行

[英]Oracle Split Delimited String Into Rows

tough one here.这里很艰难。 We are running 11g, so we do not have JSON support right now.我们正在运行 11g,所以我们现在没有 JSON 支持。 My goal is to split a JSON string on a delimiter into rows, then filter on each of those rows.我的目标是将分隔符上的 JSON 字符串拆分成行,然后过滤每一行。 Is this possible?这可能吗? Here is an example:下面是一个例子:

{
   "section_1": {
      "section_publish": true,
       "section_body": "<p style=\"text-align: justify;\">
    },
   "section_2": {
      "section_publish": false,
       "section_body": "<p style=\"text-align: justify;\">
    },
   "section_3": {
      "section_publish": true,
       "section_body": "<p style=\"text-align: justify;\">
    }
}

So, basically I am hoping to split the sections, on }, Then, once those are in "rows" filter on "section_publish": true, then filter on those.所以,基本上我希望在 } 上拆分这些部分,然后,一旦这些部分在“section_publish”上的“行”过滤器中:true,然后过滤这些部分。

The JSON strings are kept in a table, so this will be part of a SELECT statement: JSON 字符串保存在表中,因此这将是 SELECT 语句的一部分:

SELECT id, name.....
FROM json_table
WHERE {json result from above} LIKE '%string to compare%';

Does it make sense?是否有意义? Can this be done in SQL?这可以在 SQL 中完成吗?

The example below may not work for all variations of JSON that you may have, but this query does work for the example JSON provided.下面的示例可能不适用于您可能拥有的所有 JSON 变体,但此查询确实适用于提供的示例 JSON。

Query询问

WITH
    json_test (json_val) AS (SELECT EMPTY_CLOB () || '{
  "section_1": {
    "section_publish": true,
    "section_body": "<p style=\"text-align: justify;\">"
  },
  "section_2": {
    "section_publish": false,
    "section_body": "<p style=\"text-align: justify;\">"
  },
  "section_3": {
    "section_publish": true,
    "section_body": "<p style=\"text-align: justify;\">"
  }
}' FROM DUAL),
    json_split
    AS
        (    SELECT TRIM ('"' FROM REGEXP_SUBSTR (json_val,
                                                  '".*"',
                                                  1,
                                                  1 + ((LEVEL - 1) * 3)))    AS section,
                    TRIM (TRIM (',' FROM SUBSTR (REGEXP_SUBSTR (json_val,
                                                                '"section_publish".*',
                                                                1,
                                                                LEVEL),
                                                 19)))                       AS section_publish,
                    REPLACE (
                        TRIM ('"' FROM TRIM (TRIM (',' FROM SUBSTR (REGEXP_SUBSTR (json_val,
                                                                                   '"section_body".*',
                                                                                   1,
                                                                                   LEVEL),
                                                                    16)))),
                        '\"',
                        '"')                                                 AS section_body
               FROM json_test
         CONNECT BY LEVEL < REGEXP_COUNT (json_val, '{'))
SELECT TO_CHAR (section)             AS section,
       TO_CHAR (section_publish)     AS section_publish,
       TO_CHAR (section_body)        AS section_body
  FROM json_split
 WHERE TO_CHAR (section_publish) = 'true';

Result结果

     SECTION    SECTION_PUBLISH                        SECTION_BODY
____________ __________________ ___________________________________
section_1    true               <p style="text-align: justify;">
section_3    true               <p style="text-align: justify;">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM