在Postgres中获得本周的星期一日期吗? [英] Get this week's monday's date in Postgres?

查看:1052
本文介绍了在Postgres中获得本周的星期一日期吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在PostgreSQL中获得本周的星期一日期?

How can I get this week's monday's date in PostgreSQL?

例如,今天是15年1月16日(星期五)。本周的星期一日期为15/12/15。

For example, today is 01/16/15 (Friday). This week's monday date is 01/12/15.

推荐答案

SELECT current_date + cast(abs(extract(dow from current_date) - 7) + 1 as int);

可以工作,尽管可能有更优雅的方法。

works, although there might be more elegant ways of doing it.

通常的想法是获取星期几, dow ,减去7,然后采用abs,这将为您提供天,直到一周结束,然后加1,直到星期一。这会给您下星期一。

The general idea is to get the current day of the week, dow, subtract 7, and take the abs, which will give you the number of days till the end of the week, and add 1, to get to Monday. This gives you next Monday.

编辑:完全弄错了问题,可以得到上一个星期一,这要简单得多。

having completely misread the question, to get the prior Monday, is much simpler:

SELECT current_date - cast(extract(dow from current_date) as int) + 1;

即,从今天的日期(星期一的天数)减去星期几加一个,回到星期一。

ie, subtract the current day of the week from today's date (the number of day's path Monday) and add one, to get back to Monday.

这篇关于在Postgres中获得本周的星期一日期吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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