Reputation: 263
I have a date in my Neo4j database that is 9-03-2021
and this date is stored as a string in Neo4j, how would I compare that this date for example is <= 9-05-2021
?
I am basically trying to return all nodes in my database where the date field is <= a certain date, or >= a certain date, where these dates are stored as strings,
thank you
Upvotes: 2
Views: 1611
Reputation: 1
I would compare them both as strings:
WHERE 9-03-2021 <= '9-05-2021'
I found it helpful to use Neo4j's date() function to convert a date stored as a string into a temporal value if you want to compare a string value to a temporal value. First I trim the string so it's formatted as YYYY-MM-DD and then I place it inside the date() function.
For example start_date in the below example stores the start date and time as a string but I only want to return values where the start date is less than today's date:
WHERE date(left(start_date, 10)) < date()
Upvotes: 0
Reputation: 30407
The better way, of course, is to store the date as a temporal property, index it, and then in your predicates on the property compare it to other temporal values.
If you're stuck with a String property, then it would be best to use strings that afford comparison in YYYY-MM-DD format, as the string comparisons in that format will match temporal comparisons.
Your current format, DD-MM-YYYY, does not afford comparisons to other strings in that format. If you are stuck with this current format for your properties, then as Tomaž said in the comments, you would need to parse this into a type that is comparable (such as by using apoc.date.parse() ) which should give you a unix epoch timestamp (long value), and you would similarly need to parse the dates that you are comparing it to so they are all of the same type.
The problem with that approach is that you cannot use indexes here, so you won't be able to speed up the lookup. So you really should consider either using temporal properties, or at least using strings in a comparable YYYY-MM-DD format.
Upvotes: 2