我在Postgres中有一列,如下所示,我只想拆分关键字旁边的字符。在以下情况下,关键字为
“患者”
Patient Mark has tested positive
New update for Patient Wilson
Discharged - Patient Thompson
我需要的结果列应该是
Mark
Wilson
Thompson
答案 0 :(得分:1)
假设您的数据在名为str
的列中尝试以下查询
SELECT
substring(split_part(str, 'Patient', 2) from '[^ ]+'::text) as patient_name
FROM
table_name
;
答案 1 :(得分:0)
您可以使用regexp_matches功能。例如:
SELECT regexp_matches('Patient Mark has tested positive', 'Patient (\S+)');
regexp_matches
----------------
{Mark}
(1 row)
SELECT regexp_matches('New update for Patient Wilson', 'Patient (\S+)');
regexp_matches
----------------
{Wilson}
(1 row)
SELECT regexp_matches('Discharged - Patient Thompson', 'Patient (\S+)');
regexp_matches
----------------
{Thompson}
(1 row)
在这种情况下,正则表达式'Patient (\S+)'
返回关键字Patient之后的所有非空格字符,直到下一个空格或字符串末尾