【问题标题】:how fetch specific values from String in db2 query如何在 db2 查询中从字符串中获取特定值
【发布时间】:2019-09-18 15:10:27
【问题描述】:

我的表的值类似于“lowValue=100,upperValue=200”。

我尝试使用 substr 和 trim 函数来修剪 lowValue= 和 upperValue= 文本。没有什么对我有用

Select contantName,constantValue from Test where contantName="test1"; 

-------------------------------------------------
 contantName         constantValue
-------------------------------------------------
 test1               lowValue=100,upperValue=200
-------------------------------------------------

如何在选择查询中只获取低值和高值。

我希望输出仅从常量值 100 和 200 中获取数字。

【问题讨论】:

    标签: sql db2 db2-luw


    【解决方案1】:

    select contantName, REPLACE(REPLACE(constantValue,'lowValue=',''), 'upperValue=','') from Test where contantName="test1";

    【讨论】:

    • 我只想获取 100 并且仅获取 200
    【解决方案2】:

    这将是所示示例的解决方案

    WITH test (constantValue) AS ( 
    SELECT 'lowValue=1000,upperValue=20000' AS constantValue FROM SYSIBM.SYSDUMMY1
    )
    SELECT constantValue
         , substr(constantValue, posstr(constantValue, 'lowValue=') + 9 , posstr(constantValue, ',') - (posstr(constantValue, '=')+1)) AS lowvalue
         , substr(constantValue, posstr(constantValue, 'upperValue=') + 11 , length(constantValue) - (posstr(constantValue, 'upperValue=')+10)) AS uppervalue
    FROM test
    

    【讨论】:

      【解决方案3】:

      REGEXP_SUBSTR 可以做到

      SELECT 
          REGEXP_SUBSTR(value, '\blowValue=(\d+)', 1, 1, 'c', 1) as "lowValue",
          REGEXP_SUBSTR(value, '\bupperValue=(\d+)', 1, 1, 'c', 1) as "upperValue"  
      FROM (VALUES 'lowValue=54321,upperValue=123') AS base (value)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多