L
L
Ler Den2018-04-07 14:58:42
PostgreSQL
Ler Den, 2018-04-07 14:58:42

Why does date comparison not work in PostgreSQL?

There is a table. It is necessary to select records from the table earlier than obj.added - incoming parameter

create table user_recording(
  "id" serial primary key,
  "userId" serial REFERENCES user (id),  
  "recordingId" serial REFERENCES recording (id) on delete cascade,
  "added" timestamp,
  unique ("userId", "recordingId")
);

I make a request:
SELECT *
    FROM user_recording
    WHERE user_recording."userId" = ${obj.userId} AND user_recording.added::timestamp <= ${obj.added}
    ORDER BY user_recording.added DESC
    LIMIT ${obj.limit};`

The format of the incoming parameter obj.added is now moment().utc().valueOf(), i.e. just timestamp 1523101760446 getting
sql error
error: operator does not exist: timestamp without time zone <= bigint


How to compare? Changing the format of the input parameter is not a problem, if that.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-04-07
@givemoneybiatch

Compare values ​​of different types - timestamp with a number. Moreover, the number you have is not seconds at all, but milliseconds.
Try replacing user_recording.added::timestampwith extract('epoch' from user_recording.added) * 1000.
Or replace ${obj.added}with to_timestamp(${obj.added / 1000}).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question