如何查找所有第一个星期六,星期日,星期一,星期二,星期五 [英] How to find all first saturday,sun day,monday,tuesday,friday

查看:144
本文介绍了如何查找所有第一个星期六,星期日,星期一,星期二,星期五的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在SQL中查找特定月份中的所有第一个,第二个,第三个和第四个星期六,星期日,星期一,星期二和星期五,输入值来自数据列?

How to find all first, second, third and four Saturday, Sunday, Monday, Tuesday and Friday in particular month in SQL, input value is from a data column?

推荐答案

这可以清楚地说明您需要做什么:
将星期一"替换为一周中的其他日期,例如星期日等.

该示例使用sysdate,您可以将其替换为数据列中的日期.

根据OP请求从链接进行测试:
This give a clear explanation on what you need to do: Finding the first or second Monday in a month[^].
You can expand it to find the third and fourth ''Monday'' in the month. When looking for the fourth make sure you do not go into the next month.

Replace ''Monday'' with the other days of the week, for Sunday, etc.

The example uses sysdate which you can replace with the date from your data column.

Test from link as per OP request:
For this example we’ll use sysdate as input but any Oracle date will work. You can also substitute any other day of the week for Monday.

The first day of the month is probably a good place to start:

SQL> select sysdate from dual;

SYSDATE
---------
18-JUL-06

SQL> select trunc(sysdate, 'MONTH') FROM DUAL;

TRUNC(SYS
---------
01-JUL-06

Now that we’ve got that we can find the first Monday with the next_day function. Of course we need to remember the next_day function looks for the next named day after the date provided so we subtract 1 day from the date in case the first is a Monday.

SQL> select next_day(trunc(sysdate, 'MONTH')-1, 'Monday') from dual;

NEXT_DAY(
---------
03-JUL-06

Now that we have the first Monday of the month we can add 7 days to find the second Monday or 14 to find the third.

SQL> select next_day(trunc(sysdate, 'MONTH')-1, 'Monday')+7 FROM dual;

NEXT_DAY(
---------
10-JUL-06

SQL> select next_day(trunc(sysdate, 'MONTH')-1, 'Monday')+14 FROM dual;

NEXT_DAY(
---------
17-JUL-06

So from here you can change the day you’re looking for or the week number you want it in.


这篇关于如何查找所有第一个星期六,星期日,星期一,星期二,星期五的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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