PostgreSQL:42883运算符不存在:没有时区的时间戳=文本 [英] PostgreSQL: 42883 Operator does not exist: timestamp without time zone = text
问题描述
我正在使用Npgsql 3.0.3.0和PetaPoco最新版本。
I am using Npgsql 3.0.3.0 and PetaPoco latest version.
当我运行此命令时:
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);
我收到以下错误:
Npgsql.NpgsqlException 42883:不存在运算符:时间戳记
,没有时区=文本
Npgsql.NpgsqlException 42883: operator does not exist: timestamp without time zone = text
我知道错误消息是说我正在尝试将时间戳记(日期)与文本进行比较,但是对我来说,将它们进行比较是完全有效的,因为我正在期望要构建以下SQL语句:
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.
推荐答案
您需要将值转换为时间戳:
You need to cast value to timestsamp:
var sql = new Sql("WHERE date_created = @0::timestamp", dateCreated.ToString("yyyy-MM-dd HH:00:00"));
这篇关于PostgreSQL:42883运算符不存在:没有时区的时间戳=文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!