[英]SQL oracle : How to get date Between today and last year
I would like to know how to get the date column that's between today and today(lastyear) Please help mee...我想知道如何获取今天和今天(去年)之间的日期列,请帮助我...
SELECT *
FROM TABLE_X
WHERE X_DATE BETWEEN (SYSDATE) AND (SYSDATE-365);
Unfortunately, both sysdate
and current_date
in Oracle have time components.不幸的是,Oracle 中的
sysdate
和current_date
都有时间组件。 If you want the time component, then fine.如果你想要时间组件,那很好。 In general, though, you probably want:
不过,一般来说,您可能想要:
where x_date >= add_months(trunc(sysdate), -12) and
x_date < trunc(sysdate)
Note: It is unclear whether you want to include the current date or not, so you might want:注意:不清楚您是否要包括当前日期,因此您可能需要:
where x_date >= add_months(trunc(sysdate), -12) + interval '1' day and
x_date < trunc(sysdate) + interval '1' day
As a starter, you have the range bounds the wrong way around (the lower bound is greater than the upper bound), so your query cannot return any row.首先,您的范围边界是错误的(下限大于上限),因此您的查询无法返回任何行。
Then: not all years have 365 days - use add_months()
instead:然后:并非所有年份都有 365 天 - 使用
add_months()
代替:
where x_date between add_months(trunc(current_date), -12) and current_date
I am unsure whether you want to take the time component into account or not.我不确定您是否要考虑时间部分。 Assuming that you want the records until today included, you would use:
假设您希望包括今天之前的记录,您将使用:
where x_date >= add_months(trunc(current_date), -12)
and x_date < trunc(current_date) + 1
If you don't want today, then use trunc(current_date)
instead of trunc(current_date) + 1
.如果您不想要今天,请使用
trunc(current_date)
而不是trunc(current_date) + 1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.