DB2在上周日的声明到即将到来的星期六

时间:2016-01-27 21:23:42

标签: sql date db2 between

我很好奇为什么这不起作用:

SELECT *
FROM TABLE
WHERE DATE_TIME_COLUMN BETWEEN
    current date - int((dayofweek(current date))-1)
    AND
    current date + int(7-(dayofweek(current date)))

当这给了我我想要的东西时:

select current date - int((dayofweek(current date))-1) days from sysibm.sysdummy1)
select current date + int(7-(dayofweek(current date))) days from sysibm.sysdummy1)

以上两个将产生我希望我的特定日期时间列位于其间的正确日期。我在这里缺少什么?

2 个答案:

答案 0 :(得分:3)

SELECT *
FROM TABLE
WHERE DATE_TIME_COLUMN BETWEEN
current date - ((dayofweek(current date))-1) DAYS
AND
current date + (7-(dayofweek(current date))) DAYS

您必须表示您正在从当前日期减少DAYS,如上所示。

答案 1 :(得分:1)

“不起作用”有点含糊:)。您的第一个查询缺少DAY部分,以告诉DB2您正在进行日期数学的哪个部分。

然而,听起来你可能想要使用WEEK标量函数:

SELECT *
FROM TABLE
WHERE WEEK(DATE_TIME_COLUMN) = WEEK(CURRENT_DATE)
  AND YEAR(DATE_TIME_COLUMN) = YEAR(CURRENT_DATE)