SQL oracle:如何获取今天和去年之间的日期
SQL oracle : How to get date Between today and last year
我想知道如何获取今天和今天(去年)之间的日期列
请帮帮我...
SELECT *
FROM TABLE_X
WHERE X_DATE BETWEEN (SYSDATE) AND (SYSDATE-365);
作为初学者,您的范围界限是错误的(下限大于上限),因此您的查询不能 return 任何行。
然后:并非所有年份都有 365 天 - 请改用 add_months()
:
where x_date between add_months(trunc(current_date), -12) and current_date
我不确定您是否要考虑时间因素。假设您想要包括直到今天的记录,您将使用:
where x_date >= add_months(trunc(current_date), -12)
and x_date < trunc(current_date) + 1
如果你不想今天,那么使用 trunc(current_date)
而不是 trunc(current_date) + 1
。
不幸的是,Oracle 中的 sysdate
和 current_date
都有时间分量。如果你想要时间分量,那很好。不过,一般来说,您可能想要:
where x_date >= add_months(trunc(sysdate), -12) and
x_date < trunc(sysdate)
注意:不清楚您是否要包括当前日期,因此您可能需要:
where x_date >= add_months(trunc(sysdate), -12) + interval '1' day and
x_date < trunc(sysdate) + interval '1' day
我想知道如何获取今天和今天(去年)之间的日期列 请帮帮我...
SELECT *
FROM TABLE_X
WHERE X_DATE BETWEEN (SYSDATE) AND (SYSDATE-365);
作为初学者,您的范围界限是错误的(下限大于上限),因此您的查询不能 return 任何行。
然后:并非所有年份都有 365 天 - 请改用 add_months()
:
where x_date between add_months(trunc(current_date), -12) and current_date
我不确定您是否要考虑时间因素。假设您想要包括直到今天的记录,您将使用:
where x_date >= add_months(trunc(current_date), -12)
and x_date < trunc(current_date) + 1
如果你不想今天,那么使用 trunc(current_date)
而不是 trunc(current_date) + 1
。
不幸的是,Oracle 中的 sysdate
和 current_date
都有时间分量。如果你想要时间分量,那很好。不过,一般来说,您可能想要:
where x_date >= add_months(trunc(sysdate), -12) and
x_date < trunc(sysdate)
注意:不清楚您是否要包括当前日期,因此您可能需要:
where x_date >= add_months(trunc(sysdate), -12) + interval '1' day and
x_date < trunc(sysdate) + interval '1' day