I am trying to extract a value from a string in a table like this below,
query table,
query_id value
1 type={"page":"page"}&parent_id=10&image=on&content=on
2 type={"page":"page"}&parent_id=self
3 type={"category":"contact"}
as you can see the parent_id is in the query and sometimes is not.
I want to extract parent_id so I get this result,
query_id page_id value
1 10 type={"page":"page"}&parent_id=10&image=on&content=on
2 self type={"page":"page"}&parent_id=self
3 null type={"category":"contact"}
I try with this query,
SELECT
*,
CAST(
SUBSTRING(
value,PATINDEX('%parent_id=%', value) + 8,(PATINDEX('%&%', substring(value,PATINDEX('%parent_id=%', value),50)) - 9)
) AS INT
) AS page_id
FROM query
however I get this error,
1064 - You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use
near 'INT ) AS page_id FROM query LIMIT 0, 30' at line 6
EDIT:
Maybe I should not be using PATINDEX
as I tested it with the query below,
SELECT
*,
SUBSTRING(
value,PATINDEX('%parent_id=%', value) + 8,(PATINDEX('%&%', substring(value,PATINDEX('%parent_id=%', value),50)) - 9)
) AS test
FROM query
I get this error,
#1305 - FUNCTION mydb.PATINDEX does not exist
EDIT:
Got my answer,
SELECT
*,
IF(LOCATE('parent_id=', value)>0,SUBSTRING_INDEX(SUBSTRING(value,LOCATE('parent_id=', value) + 10),'&',1),null)AS page_id
FROM query