Postgres Insert Timestamp Loses Time Portion - postgresql

I have done the research and haven't found any solution to the following issue.
I wrote a small utility to move data from SQLServer to Postgres using .NET Core and EF Core. Everything works fine except after data transfer Postgres timestamp loses time portion. Source class field as well as destination defined as DateTime. Context dataset before update indicates date correctly - that is including time like so: '{8/8/2018 6:04:15 AM}'. But when I call SaveChanges() and review data in target Postgres DB - date shows up without time portion: "2018-08-08 00:00:00".
This happens with TZ or without. Inserting the data using query, results in correct timestamp. So, it looks like the problem with Npgsql.EntityCore.PostgreSQL adapter to me...
So, has anyone encountered any similar issue? Any ideas, tips are greatly appreciated!

Related

Azure Data Factory - querying Cosmos DB using dynamic timestamps

I want to create and maintain a snapshot of a collection in Cosmos DB.
Periodically, I want to retrieve only the delta (new or modified documents) from Cosmos and write them to the snapshot, which will be stored in a Azure Data Explorer cluster.
I wish to get the delta using the _ts member of the documents. In other words, I will fetch only records for which the _ts is between some range.
The range will be the range of a time window, which I get using a tumbling window trigger in the data factory.
The issue is that if I print the dynamic timestamps which I create in the query, and hard code them into the query, it works. But if I let the query generate them, I don't get any results.
For example:
I'm using those value to simulate the window range of the trigger.
I use this query to create timestamps in unix time.
and I see that the timestamps created are correct.
And if I run my query using those hardcoded timestamps, I get results
But, if I run a query using the code that just create those timestamps, I get no results from the query
This is the code to create the timestamps:
select
DateTimeToTimestamp('#{formatDateTime('2020-05-20T12:00:00.0000000Z','yyyy-MM
ddTHH:mm:ss.fffffffZ')}')/1000,
DateTimeToTimestamp('#{formatDateTime('2020-08-20T12:00:00.0000000Z','yyyy-MM
ddTHH:mm:ss.fffffffZ')}')/1000
Does anyone have a clue as to what might be the issue?
Any other way to achieve this is also welcome.
Thanks
EDIT: I managed to work around this by taking the other, simpler option:
where TimestampToDateTime(c._ts*1000)> "#{formatDateTime(pipeline().parameters.windowStart,'yyyy-MM-ddTHH:mm:ss.fffffffZ')}"
We are glad that you resolved this problem:
You managed to work around this by taking the other, simpler option:
where TimestampToDateTime(c._ts*1000)> "#{formatDateTime(pipeline().parameters.windowStart,'yyyy-MM-ddTHH:mm:ss.fffffffZ')}"
I think the error in first option is most caused by the different data type between c.ts and DateTimeToTimestamp('#{formatDateTime('2020-05-20T12:00:00.0000000Z','yyyy-MM ddTHH:mm:ss.fffffffZ')}')/1000.

PostgreSQL database causing loss of datetime-values

I have a PostgreSQL database containing a table with several 'timestamp with timezone' fields.
I have a tool (DBSync) that I want to use to transfer the contents of this table to another server/database.
When I transfer the data to a MSSQL server all datetime values are replaced with '1753-01-01'. When I transfer the data to a PostgreSQL database all datetime values are replaced with '0001-01-01'.
The smallest possible date for those systems.
Now i recreate the source-table (including contents) in a different database on the same PostgreSQL server. The only difference: the sourcetable is in a different database. Same server, same routing. Only ports are different.
User is different but in each database I have the same rights.
How can it be that the database is responsible for an apparant different interpretation of the data? Do PostgreSQL databases have database-specific settings that can cause such behaviour? What database-settings can/should I check?
To be clear, I am not looking for another way to transfer data. I have several available. The thing that I am trying to understand is: how can it be that, if an application reads datetime info from table A in database Y on server X, it gives me the the wrong date while when reading the same table from database Z on server X will give me the data as it should be.
It turns out that the cause is probably the difference in server-version. One is a Postgres 9 (works ok), the other is a Postgres 10 (does not work okay).
They are different instances on the same machine. Somehow I missed that (blush).
With transferring I meant that I am reading records from a sourcedatabase (Postgresql) and inserting them in a targetdatabase (mssql 2017).
This is done through the application, I am not sure what drivers it is using.
I wil work with the people who made the application.
For those wondering: it is this application: https://dbconvert.com/mssql/postgresql/
When a solution is found I will update this answer with the found solution.

Update core data database based on criteria using NSBatchUpdate function

I couldn't find anywhere for the answer. I have a database implemented with simple core data and an entity filled with data. Every time I open the app, I need to go through all database and one column contains a date. If the date is already passed (compared to today) I need to add one year to it.
After googling I found that NSBatchUpdate could help here. But how to go through all records in the database and update date value based on criteria?
Thanks in advance for support.

Microstrategy 10.2 Repopulate Relate Tables

I have a problem, with no errors or nothing wrong in logs.
I'm trying to repopulate relate tables with Enterprise Manager but the tables aren't being loaded correctly, only the default value appears.
The mechanism I'm using is simple: if a date is bigger then another, the process executes.
But even when I manually changed the dates with a SQL query, the process doesn't give me any results.
I also created a data load with the option to repopulate and still no changes.
Even tried to apply the fix to the 10.1 version and still nothing... (https://community.microstrategy.com/s/article/KB268380-In-MicroStrategy-10-Enterprise-Manager-Lookup-tables-in)
If any of you have any idea I'd be grateful.

Making Entity Framework generate SQL with column=GetUtcDate()

Our app runs on many web servers. The time of these web servers can get skewed over time, as is to be expected. The database is a single separate machine with it's own time. We're using EF 5.0 and have a table that needs very precise and consistent times in multiple columns. I would like to be sure the date columns in this table always use the database servers time.
In SQL I would just set the column to GetUtcDate(). Simple, the date is computed and set on the database server, done. But how can I do this with EF on an insert or update? To be clear I need the SQL generated by EF to set the column to the function GetUtcDate() so that the value comes from the database server. I do not want the date being calculated on the web server. Some ideas I've seen and considered and why they don't work for me:
1) I could use default values on the columns in the schema. But I have many update scenarios where I also need consistent dates, not just inserts.
2) I could use triggers in the database. But we currently have zero logic in our database (we are using an ORM after all) and I don't want to set that precedent if I can avoid it. It also is tricky to determine when to update these columns on the database end.
3) I can get the database server time manually (separate query as in the example below), set the column to that value, then do the update. But this is very inefficient as it requires an extra call to the database. In a tight loop this is way too much overhead. Plus the time is now less accurate since I got the time milliseconds earlier, though it is at least consistent.
CreateQuery<DateTime>("CurrentUtcDateTime()").Execute().First();
So what is the right way to do this? Or is it even possible to make EF do the right thing here?
This question is really, Can I tell EF to get the Date/Time from the DB / Underlying provider. As far as I know, this isnt possible with EF statements no.
You should use a simple SQL statement prior to the Get DBTime
T-SQL GetDate Choose the preferred date option
var dq = context.Database.SqlQuery<DateTime>("select GETUTCDATE();");
DateTime serverDate;
foreach (var dt in dq) {
serverDate = dt;
}
Now use serverDate in your EF linq statement.