如何从db2查询中的字符串中获取特定值

时间:2019-09-18 15:10:27

标签: sql db2 db2-luw

我的表具有这样的值:“ 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中获取数字。

3 个答案:

答案 0 :(得分:0)

从Test中选择contantName,REPLACE(REPLACE(constantValue,'lowValue =',''),'upperValue =','')其中contantName =“ test1”;

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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)