从DB2查询中传递的日期中排除周末

时间:2019-09-06 10:22:23

标签: sql db2

是否可以从where子句具有日期参数的查询中删除周末?

select * from table_name where startdate > '2019-01-01' and enddate < '2019-01-31'
Date             Day          Asset Price
01-01-2019      Tuesday      A        5
02-01-2019      Wednesday    B        20
03-01-2019      Thursday     C        87
04-01-2019      Friday       D        34
05-01-2019      Saturday     E        12
06-01-2019      Sunday       F       214
07-01-2019      Monday       G        32
08-01-2019      Tuesday      H        45
09-01-2019      Wednesday    I        67

因此,当运行以上查询时,资产E和F不应出现在输出中。

2 个答案:

答案 0 :(得分:2)

您在此处使用了DAYNAME函数以避免周末-

select *
from table_name
where startdate > '2019-01-01' and enddate < '2019-01-31'
and upper(dayname(Date)) not in ('SATURDAY', 'SUNDAY');

答案 1 :(得分:1)

CURRENT LOCALE LC_TIME并不理想,因为其结果取决于您的DAYOFWEEK设置。 DAYOFWEEK_ISOselect * from table_name where startdate > '2019-01-01' and enddate < '2019-01-31' and DAYOFWEEK_ISO("Date") < 6; 会更好。例如

{{1}}