PostgreSQL:42883 运算符不存在:没有时区的时间戳 = 文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42033881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:30:58  来源:igfitidea点击:

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

c#postgresqlnpgsqlpetapoco

提问by AndreFeijo

I am using Npgsql 3.0.3.0 and PetaPoco latest version.

我正在使用 Npgsql 3.0.3.0 和 PetaPoco 最新版本。

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

Npgsql.NpgsqlException 42883:运算符不存在:没有时区的时间戳 = 文本

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 expectingthe following SQL statement to be built:

我知道错误消息是说我正在尝试将时间戳(日期)与文本进行比较,但是对我来说,比较它们是完全有效的,因为我期望构建以下 SQL 语句:

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.

出于性能原因,我真的不想将我的数据库列类型转换为文本。

采纳答案by Roman Tkachuk

You need to cast value to timestsamp:

您需要将值转换为时间戳:

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

回答by Shay Rojansky

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

正如@roman-tkachuk 回答的那样,您可以告诉 PostgreSQL 将您的字符串转换为时间戳。

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 dateCreateddirectly, without the ToString().

或者更好的是,您可以直接向 PostgreSQL 发送时间戳,而不是发送时间戳的字符串表示并进行转换。为此,只需dateCreated直接发送,无需ToString().

回答by Rajesh Jaiswal

You need to explicit typecast text to timestamp.

您需要将文本显式转换为时间戳。

Try using :

尝试使用:

TO_TIMESTAMP(date_string,'YYYY-MM-DD')