如何结束一天? [英] How to get the end of a day?

查看:98
本文介绍了如何结束一天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PostgreSQL 8.4 。我在表 my_tbl 的列中包含日期(不带时区的时间戳)。例如:

 日期
------------------ -
2014-05-27 12:03:20
2014-10-30 01:20:03
2013-10-19 16:34:34
2013-07 -10 15:24:26
2013-06-24 18:15:06
2012-07-14 07:09:14
2012-05-13 04:46:18
2013-01-04 21:31:10
2013-03-26 10:17:02

如何编写以以下格式返回所有日期的SQL查询:

  xxxx-xx-xx 23 :59:59 

每个日期都将设置为当天结束。

解决方案

获取日期,截断日期,添加一天并减去一秒:

 选择date_trunc('day',date)+间隔'1 day'-间隔'1秒'

如果要更改表中的数据,可以将逻辑放入更新



当然,您也可以添加24 * 60 * 60-1秒:

  select date_trunc( '日期',日期)+( 24 * 60 * 60-1)*间隔'1秒'

但这似乎不太优雅。 / p>

I'm using PostgreSQL 8.4. I have a column of the table my_tbl which contains dates (timestamp without timezone). For instance:

       date
-------------------
2014-05-27 12:03:20
2014-10-30 01:20:03
2013-10-19 16:34:34
2013-07-10 15:24:26
2013-06-24 18:15:06
2012-07-14 07:09:14
2012-05-13 04:46:18
2013-01-04 21:31:10
2013-03-26 10:17:02

How to write an SQL query which returns all dates in the format:

xxxx-xx-xx 23:59:59

That's every date will be set to the end of the day.

解决方案

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.

这篇关于如何结束一天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆