agingcabbage32
agingcabbage32

Reputation: 364

Can i get current time using 'now()' on table with no rows?

The problem is I am using the queue: SELECT now() AT TIME ZONE :param as current_time, recording_time FROM queue ORDER BY recording_time asc LIMIT 1 to get row with earliest creation time and current time on the server database. When I have no rows in a table I get null as a result, I can process this event in the java application code, but I wonder if I can process it on psql level


Can I somehow make it so now() reterns current time despite having any rows in the table?

Upvotes: 0

Views: 27

Answers (1)

JGH
JGH

Reputation: 17836

Instead of ordering the table and getting the first record, you can directly fetch the smallest date

SELECT now() as current_time, min(recording_time) FROM queue;
         current_time          | min
-------------------------------+-----
 2021-03-18 10:40:28.991541-04 |
(1 row)

Upvotes: 2

Related Questions