vertica提取json字段值

json字符串的内容如下:

[{"stockName":"阳光照明","stockProfit":"5500.0000","stockCode":"600261"},{"stockName":"京 运 通","stockProfit":"6664.5000","stockCode":"601908"}]

如果需要提取出json里的前3个stockName,可以通过regexp_substr函数实现。如下:

select
  substr(regexp_substr(f1, '"stockName":"[w|s]+', 1, 1), 14) as stockName1,
  substr(regexp_substr(f1, '"stockName":"[w|s]+', 1, 2), 14) as stockName2,
  substr(regexp_substr(f1, '"stockName":"[w|s]+', 1, 3), 14) as stockName3

语法:

REGEXP_SUBSTR( string, pattern [, position [,  occurrence  [, regexp_modifier...  [, captured_subexp ] ] ] ])

其中,参数occurrence非常关键,当正则表达式匹配出多个子字符串时,occurrence参数表示返回第几个子字符串。

这里是关于regexp_substr函数的官方说明:

https://www.vertica.com/docs/8.1.x/HTML/index.htm#Authoring/SQLReferenceManual/Functions/RegularExpressions/REGEXP_SUBSTR.htm%3FTocPath%3DSQL%2520Reference%2520Manual%7CSQL%2520Functions%7CRegular%2520Expression%2520Functions%7C_____9

原文地址:https://www.cnblogs.com/lavezhang/p/12191852.html