I’m adding temporal Cypher functions to AGE, and I’ve implemented the date()
Cypher function. Currently, it works, and my result is printed in the output as a string.
SELECT * FROM cypher('graph', $$
RETURN date()
$$) as (date agtype);
date
--------------
"13-08-2023"
(1 row)
However, I’m concerned that functions like this may by used in SQL expressions.
SELECT t.name FROM employees AS t
WHERE t.created_date > (
SELECT a FROM cypher('graph_name', $$
RETURN date()
$$) as (created agtype) - 7); -- Created at least 7 days ago.
But PostgreSQL would treat them as strings instead of dates.
Will a new return type, agtype_value_type.AGTV_DATE
, need to be created? Or is it possible to work with strings which could be cast to PostgreSQL’s date
type?
3
Answers
I found a solution to this, thanks to Adrian's comment and ahmed_131313's answer.
We have to convert the returned record from AGE into one of PostgreSQL's character types (
char
,varchar
, ortext
), and then convert them to dates.The following SQL scripts work.
I think there is no need to create a data type to deal with such issue, you can simply convert the string you get from the cypher function into
date
ortimestamp
just by usingTO_DATE
orTO_TIMESTAMP
function as follows:and then after the conversion, use it in any calculations you wish.
I hope that was helpful, let me know how it goes with you
You can pretty much convert the string date returned by the cypher query into the PostgreSQL traditional
date
data type. Using theTO_DATE(date_text, format)
function.. check out this article for more info as to what functions you can use and how:.