PostgreSQL: 42883 Operator does not exist: timestamp without time zone = text

╄→尐↘猪︶ㄣ 提交于 2019-12-07 08:10:39

问题


I am using Npgsql 3.0.3.0 and PetaPoco latest version.

When I run this command:

var dateCreated = DateTime.Now; // just an example
var sql = new Sql("WHERE date_created = @0", dateCreated.ToString("yyyy-MM-dd HH:00:00"));
var category = db.SingleOrDefault<Category>(sql);

I get the following error:

Npgsql.NpgsqlException 42883: operator does not exist: timestamp without time zone = text

I understand the error message is saying I'm trying to compare a timestamp (date) with a text, however for me it's perfectly valid to compare them as I am expecting the following SQL statement to be built:

SELECT * FROM category WHERE date_created = '2017-02-03 15:00:00'

I don't really want to typecast my database column to text for performance reasons.


回答1:


You need to cast value to timestsamp:

var sql = new Sql("WHERE date_created = @0::timestamp", dateCreated.ToString("yyyy-MM-dd HH:00:00"));



回答2:


As @roman-tkachuk answered, you can tell PostgreSQL to cast your string into a timestamp.

Or better yet, you can simply send a timestamp to PostgreSQL directly, rather than sending a string representation of a timestamp and having it cast. To do that, simply send dateCreated directly, without the ToString().



来源:https://stackoverflow.com/questions/42033881/postgresql-42883-operator-does-not-exist-timestamp-without-time-zone-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!