Teradata SQL - 在 string/column 中的每个标签之间查找值
Teradata SQL - find values between every tags from string/column
我想提取标签之间的值并从中创建新列。
例如我的列(varchar)得到以下值:
Working : History 0 : <Site Details.Number of Complaints>WAS<>IS<3>
我需要从中提取 3 列:
1. 投诉网站 Details.Number
2.空白(空)
3. 3
因为三个值被包含在开始标记('>')和结束标记('<')之间。
我已经尝试使用 regex_substr 和 strtok,但我无法将第二个值提取为 null。
目前查询:
select STRTOK(STRTOK('Working : History 0 : <Site Details.Number of Complaints>WAS<>IS<3>','<',1),'>',1) col_a,
STRTOK(STRTOK('Working : History 0 : <Site Details.Number of Complaints>WAS<>IS<3>','<',2) ,'>',1)col_b,
STRTOK(STRTOK('Working : History 0 : <Site Details.Number of Complaints>WAS<>IS<3>','<',3) ,'>',1)col_c,
STRTOK(STRTOK('Working : History 0 : <Site Details.Number of Complaints>WAS<>IS<3>','<',4) ,'>',1)col_d
输出:
col_a col_b col_c col_d
1 Working : History 0 : Site Details.Number of Complaints IS 3
仅供参考 - 每列都有确切的 3 个开始和结束标记。我同样需要 teradata SQL。
如您所见,STRTOK
不能用于此目的,它用于使用非常基本的规则对字符串进行标记。
您需要一个正则表达式:
SELECT
RegExp_Substr(col, '<\K.*?(?=>)',1,1)
,RegExp_Substr(col, '<\K.*?(?=>)',1,2)
,RegExp_Substr(col, '<\K.*?(?=>)',1,3)
,'Working : History 0 : <Site Details.Number of Complaints>WAS<>IS<3>' AS col
<\K.*?(?=>)
<\K = check for '<', but don't add it to the result (similar to a positive lookbehind, which will not work in this case)
.*? = any characters, i.e. the expected result
(?=>) = check for '>' without adding it to the result, i.e. positive lookahead
详情见RegEx101。
我想提取标签之间的值并从中创建新列。
例如我的列(varchar)得到以下值:
Working : History 0 : <Site Details.Number of Complaints>WAS<>IS<3>
我需要从中提取 3 列: 1. 投诉网站 Details.Number 2.空白(空) 3. 3
因为三个值被包含在开始标记('>')和结束标记('<')之间。
我已经尝试使用 regex_substr 和 strtok,但我无法将第二个值提取为 null。
目前查询:
select STRTOK(STRTOK('Working : History 0 : <Site Details.Number of Complaints>WAS<>IS<3>','<',1),'>',1) col_a,
STRTOK(STRTOK('Working : History 0 : <Site Details.Number of Complaints>WAS<>IS<3>','<',2) ,'>',1)col_b,
STRTOK(STRTOK('Working : History 0 : <Site Details.Number of Complaints>WAS<>IS<3>','<',3) ,'>',1)col_c,
STRTOK(STRTOK('Working : History 0 : <Site Details.Number of Complaints>WAS<>IS<3>','<',4) ,'>',1)col_d
输出:
col_a col_b col_c col_d
1 Working : History 0 : Site Details.Number of Complaints IS 3
仅供参考 - 每列都有确切的 3 个开始和结束标记。我同样需要 teradata SQL。
如您所见,STRTOK
不能用于此目的,它用于使用非常基本的规则对字符串进行标记。
您需要一个正则表达式:
SELECT
RegExp_Substr(col, '<\K.*?(?=>)',1,1)
,RegExp_Substr(col, '<\K.*?(?=>)',1,2)
,RegExp_Substr(col, '<\K.*?(?=>)',1,3)
,'Working : History 0 : <Site Details.Number of Complaints>WAS<>IS<3>' AS col
<\K.*?(?=>)
<\K = check for '<', but don't add it to the result (similar to a positive lookbehind, which will not work in this case)
.*? = any characters, i.e. the expected result
(?=>) = check for '>' without adding it to the result, i.e. positive lookahead
详情见RegEx101。