10

I have an oracle table that store transaction and a date column. If I need to select records for one year say 2013 I do Like this:

select * 
  from sales_table
 where tran_date >= '01-JAN-2013'
   and tran_date <= '31-DEC-2013'

But I need a Straight-forward way of selecting records for one year say pass the Parameter '2013' from an Application to get results from records in that one year without giving a range. Is this Possible?

4

5 回答 5

35

使用extract函数从日期中提取年份:

select * from sales_table
where extract(YEAR from tran_date) = 2013
于 2013-08-15T09:07:17.573 回答
8

您可以使用to_date功能

http://psoug.org/reference/date_func.html

select *
  from sales_table
 where tran_date >= to_date('1.1.' || 2013, 'DD.MM.YYYY') and 
       tran_date < to_date('1.1.' || (2013 + 1), 'DD.MM.YYYY')

具有显式比较(tran_date >= ... and tran_date < ...)的解决方案能够在字段上使用索引。tran_date

考虑边界:例如,如果tran_date = '31.12.2013 18:24:45.155'您的代码tran_date <='31-DEC-2013'错过

于 2013-08-15T09:11:43.937 回答
0
select FIRST_NAME , to_char(hire_date, 'YYYY') YR FROM employees where to_char(hire_date, 'YYYY')= '2006'
于 2020-11-14T21:58:29.107 回答
0
select last_name,hire_date 
from employees
where extract(year from hire_date) = 2006;
于 2018-01-27T14:45:00.270 回答
0
select * from table_name where YEAR(date_column_name) = 2019
于 2021-11-08T06:43:22.260 回答