How to get the end of a day?

亡梦爱人 提交于 2019-11-29 10:59:36

Take the date, truncate it, add one day and subtract one second:

select date_trunc('day', date) + interval '1 day' - interval '1 second'

You can put the logic in an update if you want to change the data in the table.

Of course, you can also add 24*60*60 - 1 seconds:

select date_trunc('day', date) + (24*60*60 - 1) * interval '1 second'

But that seems less elegant.

Many ways.

I wouldn't call a timestamp column "date", that's misleading. It's also a reserved word in standard SQL and a basic type name in Postgres and shouldn't be used as identifier at all. Using column name ts instead.

1. Cast to date. Shorter than date_trunc(). Then add 1 (integer) before you subtract the interval 1 second:

ts::date + 1 - interval '1 sec' AS last_sec_of_day FROM my_tbl;

2. Add an interval '1 day - 1 sec'. No need for two operations, Postgres interval input can take both in one step:

ts::date + interval '1 day - 1 sec' AS last_sec_of_day

3. Simpler yet, add the desired time component to the date:

ts::date + time '23:59:59' AS last_sec_of_day

4. However, xxxx-xx-xx 23:59:59 is not the end of the day. The Postgres timestamp data type (currently, but unlikely to change) stores values with microsecond resolution.
The latest possible timestamp for a day is xxxx-xx-xx 23:59:59.999999:

ts::date + interval '1 day - 1 microsecond' AS last_ts_of_day

5. Equivalent:

ts::date + time '23:59:59.999999' AS last_ts_of_day  -- or interval, same result

This last expression should be fastest besides being correct.

6. However, the superior approach typically is to operate with the start of the next day as exclusive upper border, which does not depend on an implementation detail and is even simpler to generate:

ts::date + 1 AS next_day_1st_ts

7. The actual first timestamp of the next day:

date_trunc('day', ts) + interval '1 day' AS next_day_1st_ts

All work in any version since Postgres 8.4 or even longer. Demo in Postgres 12:

db<>fiddle here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!