简体   繁体   English

从字符串中提取数据 - SQL

[英]Extracting data from string - SQL

In BigQuery, I have a table with a column that produces long string values similar to the one provided below.在 BigQuery 中,我有一个表,其中有一列生成类似于下面提供的长字符串值。 There are two main parts of the string: cust_no and comp_no.字符串有两个主要部分:cust_no 和 comp_no。 Each part contains a "value" and "updated_at_ms".每个部分都包含一个“值”和“updated_at_ms”。 I am trying to extract these both cust_no and comp_no with their "value" as two new columns.我正在尝试将 cust_no 和 comp_no 与它们的“值”提取为两个新列。 "Updated_at_ms" is not necessary. “Updated_at_ms”不是必需的。

{"$cust_no": {"value": "90164F59-1120-4F2B-811D-7FEDE3CEF701", "updated_at_ms": 1600301818327}, "$comp_no": {"value": "1548715734691-5404642", "updated_at_ms": 1600301818327}}

Anyone know how I can do this extraction?有谁知道我怎么做这个提取? Any help is appreciated.任何帮助表示赞赏。 Thank you in advance.先感谢您。

Use json_value :使用json_value

with mytable as (
  select '{"$cust_no": {"value": "90164F59-1120-4F2B-811D-7FEDE3CEF701", "updated_at_ms": 1600301818327}, "$comp_no": {"value": "1548715734691-5404642", "updated_at_ms": 1600301818327}}' as col
)
select
  json_value(col, '$."$cust_no".value'),
  json_value(col, '$."$comp_no".value'),
from mytable

在此处输入图像描述

Sorry for not having a full answer.很抱歉没有完整的答案。

If I understand correctly, you want to take "the good parts" from a single column.如果我理解正确,您想从单列中获取“好的部分”。

A direction would be to use patindex and maybe substring .一个方向是使用patindex ,也许substring

SELECT SUBSTRING(MyCol, (PATINDEX('%"$cust_no": {"value": "%',[MyCol])),length)

Where length is computed with PATINDEX of ", "updated_at_ms" or something like that其中长度是用", "updated_at_ms"或类似的PATINDEX计算的

Example from here: SQL Server String extract based on pattern此处的示例: SQL 服务器字符串提取基于模式

substring API: SUBSTRING(string, start, length) substring API:SUBSTRING(字符串,开始,长度)

patIndex API: PATINDEX(%pattern%, string) patIndex API: PATINDEX(%pattern%, string)

Direction for computing length: substring of variable length计算长度方向: substring 变长

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

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