Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 574
AndreFeijo Avatar asked Feb 03 '17 22:02

AndreFeijo


People also ask

How do I call a PostgreSQL function without time zone?

Calling a PostgreSQL function which returns void and accepts various text, timestamp without time zone, jsonb, bigint, smallint etc types returns an exception when called via Dapper. The same PostgreSQL function called directly via NpgsqlCommand where each field is also defined with the correct NpgsqlDbType works as expected.

What is the difference between clock_timestamp and timeofday in PostgreSQL?

clock_timestamp() returns the actual current time, and therefore its value changes even within a single SQL command. timeofday() is a historical PostgreSQL function. Like clock_timestamp() , it returns the actual current time, but as a formatted text string rather than a timestamp with time zone value.

What does Npgsql error 42883 mean?

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:

Why doesn't PostgreSQL use UTC?

(Technically, PostgreSQL does not use UTC because leap seconds are not handled.) The number of the ISO 8601 week-numbering week of the year. By definition, ISO weeks start on Mondays and the first week of a year contains January 4 of that year. In other words, the first Thursday of a year is in week 1 of that year.


1 Answers

You need to cast value to timestsamp:

var sql = new Sql("WHERE date_created = @0::timestamp", dateCreated.ToString("yyyy-MM-dd HH:00:00"));
like image 140
Roman Tkachuk Avatar answered Nov 14 '22 23:11

Roman Tkachuk