I have a table like
memberid content
1 hello @abc
2 I'm happy @sunny @rainy
My expected output
memberid text_after_@
1 abc
2 sunny
2 rainy
I have a table like
memberid content
1 hello @abc
2 I'm happy @sunny @rainy
My expected output
memberid text_after_@
1 abc
2 sunny
2 rainy
SELECT memberid,
Min(Substr(content,
INSTR(CONTENT,
'@')
, Length(content))) over
(Partition by Substr(content,
INSTR(CONTENT,
'@'), Length(content)) order by
memberid)
from
table
This may be an ugly way of doing it, but works.
WITH RECURSIVE CTE(memberid,c1,n,cnt) AS (
SELECT MEMBERID,
SUBSTRING(content FROM (LOCATE('@',content))+1) as c1,
1 as n,
LENGTH(content)-LENGTH(REPLACE(content ,'@','')) as cnt FROM TEST
union all
SELECT MEMBERID,
SUBSTRING(c1 FROM (LOCATE('@',c1))+1),
n+1,
cnt FROM cte
where n<cnt
)
SELECT DISTINCT MEMBERID,
SUBSTRING_INDEX(c1, ' ', 1) AS TEXT_AFTER
FROM CTE
ORDER BY MEMBERID