简体   繁体   English

SQL oracle:如何获取今天和去年之间的日期

[英]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 中的sysdatecurrent_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.

相关问题 SQL 查询查找去年的日期等于今天的日期 - SQL query to find dates of last year equivalent to today's date 如何获取一列oracle sql中两个日期之间的确切年、月、日 - How to get the exact year,month,date between two dates in one column oracle sql 如何从 oracle sql 中的最大日期获取年份 - how to get the year from the max date in oracle sql 如何使用SQL获取会计年度的所有月份的最后日期 - How to get all the month last date in the fiscal year using sql 如何在 Oracle PLSQL 中获取给定年份所有十二个月的第一个日期和最后一个日期? - How to get first date and last date of all twelve months for given year in Oracle PLSQL? 如何在SQL中以格式112获取一年的第一天和一年的最后一天之间的范围 - How to get the range between first day of the year and the last day of the year in format 112 in SQL 如何获得从今天到最近7天日期的最近一周的日期 - how to get last weeek dates from today to last 7 days date 如何从HQL for Oracle中获取日期开始的年份 - how to get year from date in HQL for Oracle 如何获取当年的第一个和最后一个日期? - How to get the first and last date of the current year? 使用 SQL 函数/程序获取上一个财政年度日期范围(去年 7 月至今年 6 月) - Using SQL Function/Procedure to get last Financial year date range (July last year to June this Year)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM