I am trying to get data from a jsonb column and need to cast an attribute from the payload to timestamp. But the query aborts when the value is null or invalid.
This is my select statement.
Select
(jsonb_path_query(AnchorNode, '$.TestDate')#>> '{}')::timestamp as TestDate
From (
Select
jsonb_path_query_first(payload, '$.node1[*].node2[*]') as AnchorNode
From TestTable
) subq1
The invalid values could be only the Date or null.
How can I change the query to handle this. There are about 7 or 8 date fields where I would need to do this
Thank you
2
Answers
It was possible to achieve above using CASE statement. It may also be done using COALESCE
OR
Maybie try to cast text inside a function and return null on exception