如何排除 SQL 中的周末?
How do I exclude weekends in SQL?
我在 Orders
table 中有一个日期列 SLA_Date
。
我的 SLA_Date
应该不包括周末(周六和周日)。应单独显示工作日的数据。
如何在 SQL 中执行此操作?
您可以尝试以下条件来过滤您的数据:
WHERE case to_char (SLA_Date, 'FmDay', 'nls_date_language=english') <> 'Saturday'
AND case to_char (SLA_Date, 'FmDay', 'nls_date_language=english') <> 'Sunday'
select *
from orders
where TRUNC (thing) - TRUNC (thing, 'IW') between 0 and 4;
您只需添加以下过滤器:
WHERE TO_CHAR(date_column, 'DY','NLS_DATE_LANGUAGE=AMERICAN') NOT IN ('SAT', 'SUN')
您的查询如下:
SELECT SLA_Date
FROM orders
WHERE TO_CHAR(SLA_Date, 'DY','NLS_DATE_LANGUAGE=AMERICAN') NOT IN ('SAT', 'SUN')
例如(WITH子句只是为了构建一个测试用例),下面的查询是只显示从1st May 2015
到31st May 2015
的工作日(即不包括周六和周日) :
SQL> WITH DATA AS
2 (SELECT to_date('05/01/2015', 'MM/DD/YYYY') date1,
3 to_date('05/31/2015', 'MM/DD/YYYY') date2
4 FROM dual
5 )
6 SELECT date1+LEVEL-1 the_date,
7 TO_CHAR(date1+LEVEL-1, 'DY','NLS_DATE_LANGUAGE=AMERICAN') day
8 FROM DATA
9 WHERE TO_CHAR(date1+LEVEL-1, 'DY','NLS_DATE_LANGUAGE=AMERICAN')
10 NOT IN ('SAT', 'SUN')
11 CONNECT BY LEVEL <= date2-date1+1;
THE_DATE DAY
--------- ---
01-MAY-15 FRI
04-MAY-15 MON
05-MAY-15 TUE
06-MAY-15 WED
07-MAY-15 THU
08-MAY-15 FRI
11-MAY-15 MON
12-MAY-15 TUE
13-MAY-15 WED
14-MAY-15 THU
15-MAY-15 FRI
18-MAY-15 MON
19-MAY-15 TUE
20-MAY-15 WED
21-MAY-15 THU
22-MAY-15 FRI
25-MAY-15 MON
26-MAY-15 TUE
27-MAY-15 WED
28-MAY-15 THU
29-MAY-15 FRI
21 rows selected.
SQL>
有关详细示例,请参阅 Generate Date, Month Name, Week Number, Day number between two dates
我在 Orders
table 中有一个日期列 SLA_Date
。
我的 SLA_Date
应该不包括周末(周六和周日)。应单独显示工作日的数据。
如何在 SQL 中执行此操作?
您可以尝试以下条件来过滤您的数据:
WHERE case to_char (SLA_Date, 'FmDay', 'nls_date_language=english') <> 'Saturday'
AND case to_char (SLA_Date, 'FmDay', 'nls_date_language=english') <> 'Sunday'
select *
from orders
where TRUNC (thing) - TRUNC (thing, 'IW') between 0 and 4;
您只需添加以下过滤器:
WHERE TO_CHAR(date_column, 'DY','NLS_DATE_LANGUAGE=AMERICAN') NOT IN ('SAT', 'SUN')
您的查询如下:
SELECT SLA_Date
FROM orders
WHERE TO_CHAR(SLA_Date, 'DY','NLS_DATE_LANGUAGE=AMERICAN') NOT IN ('SAT', 'SUN')
例如(WITH子句只是为了构建一个测试用例),下面的查询是只显示从1st May 2015
到31st May 2015
的工作日(即不包括周六和周日) :
SQL> WITH DATA AS
2 (SELECT to_date('05/01/2015', 'MM/DD/YYYY') date1,
3 to_date('05/31/2015', 'MM/DD/YYYY') date2
4 FROM dual
5 )
6 SELECT date1+LEVEL-1 the_date,
7 TO_CHAR(date1+LEVEL-1, 'DY','NLS_DATE_LANGUAGE=AMERICAN') day
8 FROM DATA
9 WHERE TO_CHAR(date1+LEVEL-1, 'DY','NLS_DATE_LANGUAGE=AMERICAN')
10 NOT IN ('SAT', 'SUN')
11 CONNECT BY LEVEL <= date2-date1+1;
THE_DATE DAY
--------- ---
01-MAY-15 FRI
04-MAY-15 MON
05-MAY-15 TUE
06-MAY-15 WED
07-MAY-15 THU
08-MAY-15 FRI
11-MAY-15 MON
12-MAY-15 TUE
13-MAY-15 WED
14-MAY-15 THU
15-MAY-15 FRI
18-MAY-15 MON
19-MAY-15 TUE
20-MAY-15 WED
21-MAY-15 THU
22-MAY-15 FRI
25-MAY-15 MON
26-MAY-15 TUE
27-MAY-15 WED
28-MAY-15 THU
29-MAY-15 FRI
21 rows selected.
SQL>
有关详细示例,请参阅 Generate Date, Month Name, Week Number, Day number between two dates