简体   繁体   English

如何获得给定日期的前一个星期五和下个星期四?

[英]How can I get the previous friday and next thursday given a date?

I am trying to query the previous thursday as well as the next friday given a certain date. 我正在尝试查询给定日期的上一个星期四以及下一个星期五。 If the date passed is friday then PrevFriday should be the current date. 如果通过的日期是星期五,则PrevFriday应该是当前日期。 Similarily if the date passed is thursday then nextThursday should be the date that was passed. 同样,如果通过的日期是星期四,则nextThursday应该是通过的日期。

I have seen some other solutions on this site that include the use of WEEKDAY() offset by some interval but this only works when the date is static. 我在此站点上看到了其他一些解决方案,包括使用WEEKDAY()偏移一定的间隔,但这仅在日期为静态时有效。 As I am going to be passing a column WorkDate through this later I need this to work for every date and not just specific dates. 稍后,我将在此传递一个WorkDate列,因此我需要在每个日期(而不只是特定日期)使用它。

I currently have a query that works ONLY for today and not a range of dates. 我目前有一个查询仅适用于今天,而不是日期范围。

1. 1。

SELECT '2019-05-16' + INTERVAL -3 - weekday('2019-05-16') DAY AS PrevFriday,
       '2019-05-16' + INTERVAL 3 - weekday('2019-05-16') DAY AS NextThursday

This code outputs the expected result but if we change the date we are passing to tomorrow: 此代码输出预期结果,但是如果我们更改传递给明天的日期,则:

2. 2。

SELECT '2019-05-17' + INTERVAL -3 - weekday('2019-05-17') DAY AS PrevFriday,
       '2019-05-17' + INTERVAL 3 - weekday('2019-05-17') DAY AS NextThursday

The EXPECTED output for the first piece of code: 第一段代码的EXPECTED输出:

PrevFriday -> 2019-05-10 , NextThursday -> 2019-05-16 PrevFriday -> 2019-05-10 ,下NextThursday -> 2019-05-16

The EXPECTED output for the second piece of code: 第二段代码的EXPECTED输出:

PrevFriday -> 2019-05-17 , NextThursday -> 2019-05-23 PrevFriday -> 2019-05-17 ,下NextThursday -> 2019-05-23

But the ACTUAL results are: 但是实际结果是:

PrevFriday -> 2019-05-10 , NextThursday -> 2019-05-16 PrevFriday -> 2019-05-10 ,下NextThursday -> 2019-05-16

Try something like this: 尝试这样的事情:

select t.*
    , t.dt + interval (7 + 3 - weekday(t.dt)) % 7 day as NextThursday
    , t.dt - interval (7 - 4 + weekday(t.dt)) % 7 day as PrevFriday
from test t

The main idea is to use the modulo operator % 7 , so we never add or subtract more than 6 days. 主要思想是使用模运算符% 7 ,因此我们加或减的时间不得超过6天。

Test Data: 测试数据:

create table test(
  d char(2),
  dt date
);
insert into test(d, dt) values
('mo', '2019-05-13'),
('tu', '2019-05-14'),
('we', '2019-05-15'),
('th', '2019-05-16'),
('fr', '2019-05-17'),
('su', '2019-05-18'),
('so', '2019-05-19');

Result: 结果:

d  | dt         | NextThursday | PrevFriday
---|------------|--------------|-----------
mo | 2019-05-13 |   2019-05-16 | 2019-05-10
tu | 2019-05-14 |   2019-05-16 | 2019-05-10
we | 2019-05-15 |   2019-05-16 | 2019-05-10
th | 2019-05-16 |   2019-05-16 | 2019-05-10
fr | 2019-05-17 |   2019-05-23 | 2019-05-17
su | 2019-05-18 |   2019-05-23 | 2019-05-17
so | 2019-05-19 |   2019-05-23 | 2019-05-17

db-fiddle demo db-fiddle演示

IF WEEKDAY(DateVar) = 4 then --Friday logic
    PrevFriday = DateVar
    NextThursday =  DATE_ADD(DateVar, INTERVAL 6 DAY);
ELSEIF WEEKDAY(DateVar) = 3 THEN --Thursday logic
    PrevFriday = DATE_ADD(DateVar, INTERVAL -6 DAY)
    NextThursday = DateVar;
ELSE
    -- logic to ind the Prev Friday and Next Thursday; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM