Last active
March 17, 2026 19:41
-
-
Save luanschmidt/9f24342d89a958ac9820455c7a31b6a5 to your computer and use it in GitHub Desktop.
Create random dates to use as a Property on Neo4J, considering the maximum amount of days in each month, not taking leap years into account.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Using this MATCH in the beginning of the query to define the type of node you are referencing will make Neo4J iterate through | |
| // each of them, avoiding the need to use other methods. | |
| // startYear is the start of the year range, endYear is the end of the year range. Change to your needs as you see fit. | |
| // Variables y and m are the random year and month generated, so you can obtain the days in the next step, that are returned as | |
| // the variable maxDays in the end of the CASE statement | |
| // With the SET command on line 15, you can either update or create the desired property containing the random date you just created. | |
| // The RETURN is optional in case you want to check the created dates. | |
| MATCH (n:LabelOfNode) | |
| WITH n, 1900 as startYear, 2010 AS endYear | |
| WITH n, toInteger(startYear + floor(rand() * (endYear - startYear + 1))) AS y, toInteger(1 + floor(rand() * 12)) AS m | |
| WITH n, y, m, | |
| CASE | |
| WHEN m IN [1, 3, 5, 7, 8, 10, 12] THEN 31 | |
| WHEN m IN [4, 6, 9, 11] THEN 30 | |
| WHEN m = 2 THEN 28 | |
| END AS maxDays | |
| SET n.desiredProperty = date({year: y, month: m, day:toInteger(1 + floor(rand() * maxDays))}) | |
| RETURN n.desiredProperty; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment