Reputation: 147
I have the following nodes with this properties:
MATCH (a:Person{name:'Raf', birthDay:'05/07/1992'}),
(b:Person{name:'Mary', birthDay:'10/08/1991'}),
(c:Person{name:'Luke', birthDay:'11/01/1995'})
How could I set the birthDay from String format to Date format? And How could I order my nodes considering birthDay and have its rank?
Upvotes: 0
Views: 3106
Reputation: 20185
The first operation to do is to split the text of the birthday to distinguished parts :
RETURN split('05/07/1992', '/') AS parts
will return an array
["05", "07", "1992"]
The second operation is to create a date object :
WITH split('05/07/1992', '/') AS parts
RETURN date({day: parts[0], month: parts[1], year: parts[2]})
The problem is that day, month and year
expect integer values, not strings :
WITH [x IN split('05/07/1992', '/') | toInteger(x)] AS parts
RETURN date({day: parts[0], month: parts[1], year: parts[2]})
╒════════════════════════════════════════════════════════╕
│"date({day: parts[0], month: parts[1], year: parts[2]})"│
╞════════════════════════════════════════════════════════╡
│"1992-07-05" │
└────────────────────────────────────────────────────────┘
Now for the second question, how to order the nodes by birthday and have its rank ?
Well, you could order by a timestamp instead, however as you can see in this documentation reference about the date type, it doesn't have the epochMillis on it, so you can rather use datetime instead.
Full flow :
// Create Persons
CREATE (a:Person{name:'Raf', birthDay:'05/07/1992'}),
(b:Person{name:'Mary', birthDay:'10/08/1991'}),
(c:Person{name:'Luke', birthDay:'11/01/1995'})
// Set datetime as dob property
MATCH (p:Person)
WITH p, [x IN split(p.birthDay, "/") | toInteger(x)] AS parts
SET p.dob = datetime({day: parts[0], month: parts[1], year: parts[2]})
// Return younger persons
MATCH (p:Person) RETURN p ORDER BY p.dob.epochMillis DESC
Upvotes: 4