user16029964
user16029964

Reputation: 13

How to check if two dates are the same with timestamp

I have two dates. One I get from the database, the other is which one I want to pass. How can I check if these two are the same? These are timestamp with time zones

In this case they are the same.

% timestamp_coloumn is my column where the value is in it
SELECT *
    FROM public.tbl_text
WHERE ('2021-06-02T06:33:51.485Z' - timestamp_coloumn) = 0;

%What I hand over: 2021-06-02T06:33:51.485Z

%Stands in the database: 2021-06-02 06:33:51.485093+00 = timestamp_coloumn

Upvotes: 1

Views: 225

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1271231

I would suggest writing this as:

WHERE timestamp_coloumn >= '2021-06-02T06:33:51.485Z'::timestamp AND
      timestamp_coloumn < '2021-06-02T06:33:51.485Z'::timestamp + INTERVAL '1 millisecond'

Note that this is index and optimizer friendly. If you have an index on timestamp_column, this might be very, very fast.

Upvotes: 0

Stefanov.sm
Stefanov.sm

Reputation: 13069

SELECT *
    FROM public.tbl_text
    WHERE 
      '2021-06-02T06:33:51.485Z'::timestamptz = 
       date_trunc('milliseconds', timestamp_coloumn);

Upvotes: 1

Related Questions