era
era

Reputation: 481

Store date in neo4j

The given date format in CSV is '(Fri) 09 Jan 2018 (32)'. This should feed to database as a date column to allow order by date. How could convert above format to Neo4j date format at the insertion time ?

Upvotes: 0

Views: 156

Answers (2)

cybersam
cybersam

Reputation: 67044

Assuming the date substring always starts at offset 6 in the input string, this will return the date offset from 01 Jan 1970, which is adequate for comparing dates:

WITH '(Fri) 09 Jan 2018 (32)' as s
RETURN apoc.date.parse(SUBSTRING(s, 6, 11), 'd', "dd MMM yyyy") as date;

Upvotes: 0

ThirstForKnowledge
ThirstForKnowledge

Reputation: 1304

Solution

WITH '(Fri) 09 Jan 2018 (32)' as inputString
WITH split(inputString, ' ') as parts
WITH parts[1] + ' ' + parts[2] + ' ' + parts[3] AS concatDate
RETURN apoc.date.parse(concatDate, 's',"dd MMM yyyy") as date;

Explanation:

  • line 1: defines a date for testing purpose
  • line 2: splits the given date into its pieces at each blank
  • line 3: concatenates the day, month and year
  • line 4: parse the built date of line 3 and convert it to a Neo4j date

Result

╒══════════╕
│"date"    │
╞══════════╡
│1515456000│
└──────────┘

Alternative solution

WITH '(Fri) 09 Jan 2018 (32)' as inputString
WITH split(inputString, ' ') as parts
WITH reduce(s = "", x IN parts[1..4] | s + x) AS concatDate
RETURN apoc.date.parse(concatDate, 's',"ddMMMyyyy") as date;

Upvotes: 1

Related Questions