Converting DateTime to Epoch milliseconds using Cypher in Neo4J - date

I'm running a query using Cypher in Neo4J where I have to compare a createdAt property of a node against a given time unit in Epoch milliseconds. This createdAt property is a string in the DateTime format, which is defined as -
DateTime
date with a precision of miliseconds, encoded as a string with the following format: yyyy-mm-ddTHH:MM:ss.sss+0000, where yyyy is
a four-digit integer representing the year, the year, mm is a
two-digit integer representing the month and dd is a two-digit integer
representing the day, HH is a two-digit integer representing the hour,
MM is a two digit integer representing the minute and ss.sss is a five
digit fixed point real number representing the seconds up to
milisecond precision. Finally, the +0000 of the end represents the
timezone, which in this case is always GMT.
Here are a couple of values of this property - 2011-03-21T19:32:38.295+0000, 2012-03-09T17:59:05.367+0000.
I came across the Temporal Values documentation on Neo4j, but couldn't find a way to perform the conversion.
When I execute some of the given examples, like this -
RETURN datetime('2015-06-24T12:50:35.556+0100') AS theDateTime
I get the error -
Neo.ClientError.Statement.SyntaxError: Unknown function 'datetime' (line 1, column 16 (offset: 15))
Would appreciate any help!

The temporal functions were added in neo4j version 3.4.0, and I have verified that your query works in that version.
Make sure you are using an appropriately recent version of neo4j.

Related

PervasiveSQL Database dates conversion

I'm working on tables obtained from a PervasiveSQL database and I have some trouble managing dates.
In some of the fields dates are recorded in the format we use in Italy, dd/mm/yyyy, but in others are recorded in a format I can't understand, something like this:
Start_Date 132384788
Last_Tx_Date 132385052
Last_Tx_Time 252711936
What kind of format is it?
How can I convert it in a human readable one?
I think that Start_Date could be August 8 2020 but I'm not sure.
Thanks for any help!
I tried to copy and paste tables in an Excel file but automatic dates conversion did not work.
The Start_Date and Last_Tx_Date fields look to be Btrieve Date fields. If you set the data type for that field in the DDFs to Date, it should show a human readable field. However the Last_Tx_Time field is a Btrieve Time (not timestamp) type.
From the Actian Zen v15.10 documentation (https://docs.actian.com/zen/v15/#page/sqlref/sqldtype.htm#ww136646):
Date:
The DATE key type is stored internally as a 4-byte value. The day and the month are each stored in 1-byte binary format. The year is a 2-byte binary number that represents the entire year value. The MicroKernel places the day into the first byte, the month into the second byte, and the year into a two-byte word following the month.
An example of C structure used for date fields would be:
TYPE dateField {
char day;
char month;
integer year;
}
The year portion of a date field is expected to be set to the integer representation of the entire year. For example, 2,001 for the year 2001.
Time:
The TIME key type is stored internally as a 4-byte value. Hundredths of a second, second, minute, and hour values are each stored in 1-byte binary format. The MicroKernel places the hundredths of a second value in the first byte, followed respectively by the second, minute, and hour values. The data format is hh:mm:ss.nn. Supported values range from 00:00:00.00 to 23:59:59.99.

How do I convert a Julian date stored as a double-precision value to a timestamp with at least one-minute resolution?

I exported data from an SQLite table to a CSV file. The data includes a timestamp with at least one-minute resolution: "2019-11-15 01:30:06". The data is actually stored as a Julian date, in this case 2458802.35424295. I imported the data into a double-precision field. I need to convert that number into a timestamp with time zone. I tried casting the double-precision number to text and then using to_timestamp(), but that appears to work only with integer days. I can get a timestamp, but it is always at midnight of the correct date. I tried using to_timestamp() passing in my number, but that returns an epoch (number of milliseconds since 1/1/1970).
I could try to take the fractional part of my Julian date value, calculate the number of milliseconds since midnight that represents, use the to_timestamp(text,text) method to get the date I need, and then add the epoch since midnight to that date. But that's awfully cumbersome. Isn't there a better way?
I'm using PostgreSQL 9.3.
NOTE: The simple answer to my problem, which occured to me just before I clicked the Post button, is to export the data in the form I want, using SQLite's datetime() function to convert the number to a date string during export. But I remain curious. I would have thought there would be a standard way to do this conversion.

BigQuery upload date error from csv file

I have been trying to upload a table with dates on the from a csv file but I keep getting an error about the date type like this:
Errors:
Too many errors encountered. (error code: invalid)
query: Invalid date: '2010-06-31' (error code: invalidQuery)
So it is complaining about 2010-06-31. I checked the reference and it says:
Date type
Name Description DATE Represents a logical calendar date. Values range
between the years 1 and 9999, inclusive. The DATE type represents a
logical calendar date, independent of time zone. A DATE value does not
represent a specific 24-hour time period. Rather, a given DATE value
represents a different 24-hour period when interpreted in different
time zones, and may represent a shorter or longer day during Daylight
Savings Time transitions. To represent an absolute point in time, use
a timestamp.
Canonical format
'YYYY-[M]M-[D]D' YYYY: Four-digit year [M]M: One or two digit month
[D]D: One or two digit day
https://cloud.google.com/bigquery/sql-reference/data-types#date-type
It says YYYY-[M]M-[D]D so I thought 2010-06-31 is correct but still getting an error.
My rows look like this in the csv file:
Regular Season,2010-06-31,Chicago,Road,22,37,21,28,,,,,108,240,39,79
My schema looks like this:
_Dataset: STRING
_DATE: DATE
_TEAMS: STRING
_VENUE: STRING
_1Q: INTEGER
_2Q: INTEGER
_3Q: INTEGER
_4Q: INTEGER
_OT1: INTEGER
_OT2: INTEGER
_OT3: INTEGER
_OT4: INTEGER
_F: INTEGER
_MIN: INTEGER
_FG: INTEGER
_FGA: INTEGER
Thanks in advance for your help
Even though June 31 exists as per The Thirty-first of June by J.B. Priestley -
your issue can be simply just because in reality - June month has only 30 days, so load engine gets stuck with June 31st
On the other hand - query engine successfully "translates" 2010-06-31 into 2010-07-01 - try below example
SELECT DATE('2010-06-31')

How do I convert date and seconds fields to a timestamp field in PostgreSQL?

I have a date field, which is just text, and a field showing seconds since midnight.
CREATE TABLE t ("s" text, "d" text, "ts" timestamp)
;
INSERT INTO t ("s", "d")
VALUES
(24855, 20130604),
(24937.7, 20130604)
;
How can I convert these fields into a timestamp, so I can run time-based queries?
See a demo with SQL Fiddle:
http://sqlfiddle.com/#!15/67018/2
Since your data includes fractional seconds, you need to adjust for those:
UPDATE t
SET ts = to_timestamp(d||s, 'YYYYMMDDSSSS.MS');
Assuming milliseconds as the maximum precision. Else employ US for microseconds.
Consider the fine print in the manual:
In a conversion from string to timestamp, millisecond (MS) or
microsecond (US) values are used as the seconds digits after the
decimal point. For example to_timestamp('12:3', 'SS:MS') is not 3
milliseconds, but 300, because the conversion counts it as 12 + 0.3
seconds. This means for the format SS:MS, the input values 12:3,
12:30, and 12:300 specify the same number of milliseconds. To get
three milliseconds, one must use 12:003, which the conversion counts
as 12 + 0.003 = 12.003 seconds.
Also note that to_timestamp() returns timestamp with time zone, which is coerced to timestamp in the context of the update, which turns out all right. For other contexts, you may want to cast explicitly or use this alternative, yielding timestamp:
SELECT d::date + interval '1s' * s::numeric AS ts FROM t;
The plain cast to date (d::date) works because your dates are in standard ISO 8601 format - or so I assume.
Concatenate the two fields together and then use to_timestamp()
UPDATE t SET ts = to_timestamp(d||s, 'YYYYMMDDSSSS');
For a discussion of the formatting string used in to_timestamp, see Table 9-21: Template Patterns for Date/Time Formatting in the PostgreSQL documentation.

Microsoft Hex dates

I have the following from a Microsoft SQL Server database for date/time value:
0x00009CEF00A25634
I found this post:
Help me translate long value, expressed in hex, back in to a date/time
Which seemed to be on the right track but by using the code I didn't get the right dates, are my hex dates in a different format? How would I convert them to a normal date, I am using PHP/PostgreSQL.
select CAST (0x00009CEF00A25634 as datetime) gives 2009-12-30 09:51:03.000
This is two integers. One for the date part 0x00009CEF (decimal 40175) and one for the time part 00A25634 (decimal 10638900). The date part is a signed integer giving number of days since 1 Jan 1900. The time part is an integer representing number of ticks.
There are 300 ticks in a second.
It can be seen that the following also returns the same result
SELECT DATEADD(MILLISECOND,10638900*10/3.0, DATEADD(DAY,40175, '19000101'))
You will need to figure out how to apply this to postgres.
Edit: an answer here apparently does this. I haven't tested it myself.
This works for me while migrating from SQL to MySQL :
SELECT (CAST('1900-01-01 00:00:00' + INTERVAL CAST(CONV(substr(HEX( 0x0000A249004576D0 ),1,8), 16, 10) AS SIGNED) DAY + INTERVAL CAST(CONV(substr(HEX( 0x0000A249004576D0 ),9,8), 16, 10) AS SIGNED)* 10000/3 MICROSECOND AS DATETIME)) AS newdate