如何解析字符串并获取“=”字符后的值

How to parse a string and get the value after "=" character

所以我的字符串应该连续一次包含“Object.Name”,如果我看到它,我必须在“=”字符之后获取值。如果在字符串中的任何地方都不匹配,我应该移动硬编码值。

这里是字符串的例子:

Object.Name=ASDD||Product.Name=DSA
Product.Name=QWE||Object.Name=WSXS
Storage.Name=12345||Object.Name=WERR||Product.Name=QAZ

我知道我应该为此使用 case,但不知道如何处理字符串

case
    when (match the string ) then (value after the "=")
    else (hardcoded value)
end

在 Oracle 中,您可以使用:

SELECT value,
       CASE
       WHEN start_pos = 0
       THEN NULL
       ELSE SUBSTR(
              '||' || value || '||',
              start_pos + LENGTH('||Object.Name='),
              end_pos - start_pos - LENGTH('||Object.Name=')
            )
       END AS object_name
FROM   (
  SELECT value,
         INSTR(
           '||' || value || '||',
           '||Object.Name='
         ) AS start_pos,
         INSTR(
           '||' || value || '||',
           '||',
           INSTR('||' || value || '||', '||Object.Name=')+LENGTH('||Object.Name=')
         ) AS end_pos
  FROM   table_name
)

其中,对于示例数据:

CREATE TABLE table_name (value) AS
SELECT 'Object.Name=ASDD||Product.Name=DSA' FROM DUAL UNION ALL
SELECT 'Product.Name=QWE||Object.Name=WSXS' FROM DUAL UNION ALL
SELECT 'Storage.Name=12345||Object.Name=WERR||Product.Name=QAZ' FROM DUAL;

输出:

VALUE OBJECT_NAME
Object.Name=ASDD||Product.Name=DSA ASDD
Product.Name=QWE||Object.Name=WSXS WSXS
Storage.Name=12345||Object.Name=WERR||Product.Name=QAZ WERR

db<>fiddle here


由于您更改了标签,在 MySQL:

SELECT value,
       CASE
       WHEN start_pos = 0
       THEN NULL
       ELSE SUBSTRING(
              CONCAT('||', value, '||'),
              start_pos + LENGTH('||Object.Name='),
              end_pos - start_pos - LENGTH('||Object.Name=')
            )
       END AS object_name
FROM   (
  SELECT value,
         LOCATE(
           '||Object.Name=',
           CONCAT('||', value, '||')
         ) AS start_pos,
         LOCATE(
           '||',
           CONCAT('||', value, '||'),
           LOCATE('||Object.Name=', CONCAT('||', value, '||'))
             + LENGTH('||Object.Name=')
         ) AS end_pos
  FROM   table_name
) t

db<>fiddle here