Saving date and time to PostgreSQL using Delphi - postgresql

I have a column in my PostgreSQL database, which is in timestamp without time zone format. I would like to save the current date there using the following code:
MyStoredProc.ParamByName('date').Value := FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Now);
The date value in my stored procedure is declared as DateTime, but setting it to PgTimeStamp does not help either. The first column is created in PostgreSQL, timestamps working correctly, but the second one, where I´m trying to save data from my code, is always showing zeros.
What am I doing wrong?

You should be able to pass directly the datetime object as parameter int his way :
MyStoredProc.ParamByName('date').Value := Now;
or :
MyStoredProc.ParamByName('date').AsDateTime := Now;
Of course the parameter of the PostgreSQL function must be timezone type.

Related

Unable to transform string to datetime/timestamp in specific format in ADF dataflow

Trying to convert string value(2022-07-24T07:04:27.5765591Z) into datetime/timestamp to insert into SQL table in datetime format without losing any value till milliseconds. String which I am providing is actually a datetime and my source is ADLS CSV. I tried below options in data flow.
Using Projection-> Changed the datatype format for specific column into timestamp and format type-yyyy-MM-dd'T'HH:mm:ss.SSS'Z' however getting NULL in output.
Derived column-> Tried below expressions but getting NULL value in output
toTimestamp(DataLakeModified_DateTime,'%Y-%m-%dT%H:%M:%s%z')
toTimestamp(DataLakeModified_DateTime,'yyyy-MM-ddTHH:mm:ss:fffffffK')
toTimestamp(DataLakeModified_DateTime,'yyyy-MM-dd HH:mm:ss.SSS')
I want the same value in output-
2022-07-24T07:04:27.5765591Z (coming as string) to 2022-07-24T07:04:27.5765591Z (in datetime format which will be accepted by SQL database)
I have tried to repro the issue and it is also giving me the same error, i.e., null values for yyyy-MM-dd'T'HH:mm:ss.SSS'Z' timestamp format. The issue is with the string format you are providing in source. The ADF isn’t taking the given string as timestamp and hence giving NULL in return.
But if you tried with some different format, like keeping only 3 digits before Z in last format, it will convert it into timestamp and will not return NULL.
This is what I have tried. I have kept one timestamp as per your given data and other with some modification. Refer below image.
This will return NULL for the first time and datetime for second time.
But the format you are looking for is still missing. With the existing source format, the yyyy-MM-dd'T'HH:mm:ss would work fine. This format also works fine in SQL tables. I have tried and it’s working fine.
Try to use to String instead of timestamp and use this to create your Desired timestamp
toString(DataLakeModified_DateTime, 'yyyy-MM-dd HH:mm:ss:SS')

How to truncate date in postgres?

I am using below condition to truncate date in postgres
to_date(to_char(trunc(appointment_date),'YYYYMMDD')||appointment_end_time,''YYYYMMDDHH24:MI:SS')AS tq
How I can use this in postgres ?
Strange data typing, sometimes requires strange, looking at least, queries. Try (see fiddle)
date_trunc('day',appointment_date)
+ substr(appoinment_end,12)::interval
As your to_char() call uses the format 'HH24:MI:SS' for the "time" column, you can cast that column directly to a time value, e.g. using the :: operator: appointment_end_time::time.
To build a new timestamp from the date part of the appointment_date and the time value, just add them:
appointment_date::date + appointment_end_time::time
So first the timestamp is converted to a date (that does not have a time), and then the time value is added to that, which yields a timestamp.
Note that to_date() returns a date so your code would remove the just added time part again. You would need to use to_timestamp() if you really want a timestamp as the result.
To answer the question's title "how to truncate date in Postgres?" (which in reality refers to a timestamp not a date): you can either cast it to a date (see above) or you can use date_trunc() (not trunc()) with a unit to which it should be truncated. However, date_trunc returns a timestamp not a date value, so you couldn't add a time to the result.

got the following error while declaring the timestamp sa data type in postgresql

My time stamp value format is like 20200203160857. I am declaring my variable time stamp as TIMESTAMP.
time_stamp TIMESTAMP NULL,
and I am copying a csv file in that the time stamp value is as shown above. While inserting i got the following error.
date/time field value out of range: "20200203160857"\n HINT: Perhaps you need a different "datestyle" setting.\n
use to_timestamp() function like below to manage your format of timestamp
to_timestamp('20200203160857', 'YYYYMMDDHH24MISS')

Neo4j - Date conversion

I'm using Neo4j database.Neo4j does not have date data type only have timestamp data type.
I need to compare current date with existing date using cql query.
My existing date format is "8/4/2011" that is string.
Then how can I compare it.Any way to use stored procedure [date] while csv bulk data import time.
I used APOC stored procedure but I don't know how compare it.
CALL apoc.date.format(timestamp(),"ms","dd.MM.yyyy")
07.07.2016
CALL apoc.date.parse("13.01.1975 19:00","s","dd.MM.yyyy HH:mm")
158871600
I expect like this
MATCH(dst:Distributor) WHERE dst.DIST_ID = "111137401" WITH dst CALL apoc.date.parse(dst.ENTRY_DATE,'s', 'dd/MM/yyyy') YIELD d SET dst.ENTRY_DATE = d RETURN dst;
Any possibilities please help me...
RETURN datetime("2018-06-04T10:58:30.007Z").epochMillis
1528109910007
Right query is :
USING PERIODIC COMMIT LOAD CSV WITH HEADERS FROM "file:///DST.csv" AS row
CALL apoc.date.parse(toString(row.ENTRY_DATE),"ms","dd-MMM-yy") YIELD value as date CREATE (DST:Distributor {ENTRY_DATE: date })

How can i insert timestamp with timezone in postgresql with prepared statement?

I am trying to insert to a timestamp with timezone field of my DB a string which includes date, time and timezone using prepared statement.
The problem is that Timestamp.valueof function does not take into consideration the time zone that the string inludes so it causes an error.
The accepted format is yyyy-[m]m-[d]d hh:mm:ss[.f...] which does not mention timezone.
That is the exact code that causes the error:
pst.setTimestamp(2,Timestamp.valueOf("2012-08-24 14:00:00 +02:00"))
Is there any way that i can overcome it??
Thanks in advance!
The basic problem is that a java.sql.Timestamp does not contain timezone information. I think it is always assumed to be "local timezone".
On solution I can think of is to not use a parameter in a PreparedStatement, but a timezone literal in SQL:
update foo
set ts_col = timestamp with time zone '2012-08-24 14:00:00 +02:00'`;
Another possible solution could be to pass a properly formatted String to a PrepareStatement that uses to_timestamp():
String sql = "update foo set ts_col = to_timestamp(?, 'yyyy-mm-dd hh24:mi:ss')";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1, "2012-08-24 14:00:00 +02:00");
I believe that you could use one more field in your database, which would include the time zone. And calculate the time manually after you get these two fields