选择特定年份Oracle的记录

时间:2013-08-15 09:03:13

标签: sql oracle11g

我有一个存储事务和日期列的oracle表。如果我需要选择一年的记录,比如2013年我喜欢这个:

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

但我需要一种直接的方式来选择一年的记录,比如从应用程序传递参数'2013',以便在一年内从记录中获得结果而不给出范围。这可能吗?

5 个答案:

答案 0 :(得分:24)

使用extract功能从日期中提取年份:

select * from sales_table
where extract(YEAR from tran_date) = 2013

答案 1 :(得分: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' 错过那么

答案 2 :(得分:0)

select last_name,hire_date 
from employees
where extract(year from hire_date) = 2006;

答案 3 :(得分:0)

select FIRST_NAME , to_char(hire_date, 'YYYY') YR FROM employees where to_char(hire_date, 'YYYY')= '2006'

答案 4 :(得分:-2)

- 只需将年份放入给定年份:CAL_YEAR即可获得年度结果日期

select daterange, dayname from 
(   
select (to_date(01||' '||:CAL_YEAR,'MM YYYY')-1 + level) DateRange,  
TO_CHAR(to_date(01||' '||:CAL_YEAR,'MM YYYY')-1 + level,'DAY') DAYNAME
from    dual
where   (to_date(01||' '||:CAL_YEAR,'MM YYYY')-1+level) <= last_day(to_date(01||' '||:CAL_YEAR,'MM YYYY'))
connect by level<=31
union
select (to_date(02||' '||:CAL_YEAR,'MM YYYY')-1 + level) DateRange,  
TO_CHAR(to_date(02||' '||:CAL_YEAR,'MM YYYY')-1 + level,'DAY') DAYNAME
from    dual
where   (to_date(02||' '||:CAL_YEAR,'MM YYYY')-1+level) <= last_day(to_date(02||' '||:CAL_YEAR,'MM YYYY'))
connect by level<=31
union
select (to_date(03||' '||:CAL_YEAR,'MM YYYY')-1 + level) DateRange,  
TO_CHAR(to_date(03||' '||:CAL_YEAR,'MM YYYY')-1 + level,'DAY') DAYNAME
from    dual
where   (to_date(03||' '||:CAL_YEAR,'MM YYYY')-1+level) <= last_day(to_date(03||' '||:CAL_YEAR,'MM YYYY'))
connect by level<=31
union
select (to_date(04||' '||:CAL_YEAR,'MM YYYY')-1 + level) DateRange,  
TO_CHAR(to_date(04||' '||:CAL_YEAR,'MM YYYY')-1 + level,'DAY') DAYNAME
from    dual
where   (to_date(04||' '||:CAL_YEAR,'MM YYYY')-1+level) <= last_day(to_date(04||' '||:CAL_YEAR,'MM YYYY'))
connect by level<=31
union
select (to_date(05||' '||:CAL_YEAR,'MM YYYY')-1 + level) DateRange,  
TO_CHAR(to_date(05||' '||:CAL_YEAR,'MM YYYY')-1 + level,'DAY') DAYNAME
from    dual
where   (to_date(05||' '||:CAL_YEAR,'MM YYYY')-1+level) <= last_day(to_date(05||' '||:CAL_YEAR,'MM YYYY'))
connect by level<=31
union
select (to_date(06||' '||:CAL_YEAR,'MM YYYY')-1 + level) DateRange,  
TO_CHAR(to_date(06||' '||:CAL_YEAR,'MM YYYY')-1 + level,'DAY') DAYNAME
from    dual
where   (to_date(06||' '||:CAL_YEAR,'MM YYYY')-1+level) <= last_day(to_date(06||' '||:CAL_YEAR,'MM YYYY'))
connect by level<=31
union
select (to_date(07||' '||:CAL_YEAR,'MM YYYY')-1 + level) DateRange,  
TO_CHAR(to_date(07||' '||:CAL_YEAR,'MM YYYY')-1 + level,'DAY') DAYNAME
from    dual
where   (to_date(07||' '||:CAL_YEAR,'MM YYYY')-1+level) <= last_day(to_date(07||' '||:CAL_YEAR,'MM YYYY'))
connect by level<=31
union
select (to_date(08||' '||:CAL_YEAR,'MM YYYY')-1 + level) DateRange,  
TO_CHAR(to_date(08||' '||:CAL_YEAR,'MM YYYY')-1 + level,'DAY') DAYNAME
from    dual
where   (to_date(08||' '||:CAL_YEAR,'MM YYYY')-1+level) <= last_day(to_date(08||' '||:CAL_YEAR,'MM YYYY'))
connect by level<=31
union
select (to_date(09||' '||:CAL_YEAR,'MM YYYY')-1 + level) DateRange,  
TO_CHAR(to_date(09||' '||:CAL_YEAR,'MM YYYY')-1 + level,'DAY') DAYNAME
from    dual
where   (to_date(09||' '||:CAL_YEAR,'MM YYYY')-1+level) <= last_day(to_date(09||' '||:CAL_YEAR,'MM YYYY'))
connect by level<=31
union
select (to_date(10||' '||:CAL_YEAR,'MM YYYY')-1 + level) DateRange,  
TO_CHAR(to_date(10||' '||:CAL_YEAR,'MM YYYY')-1 + level,'DAY') DAYNAME
from    dual
where   (to_date(10||' '||:CAL_YEAR,'MM YYYY')-1+level) <= last_day(to_date(10||' '||:CAL_YEAR,'MM YYYY'))
connect by level<=31
union
select (to_date(11||' '||:CAL_YEAR,'MM YYYY')-1 + level) DateRange,  
TO_CHAR(to_date(11||' '||:CAL_YEAR,'MM YYYY')-1 + level,'DAY') DAYNAME
from    dual
where   (to_date(11||' '||:CAL_YEAR,'MM YYYY')-1+level) <= last_day(to_date(11||' '||:CAL_YEAR,'MM YYYY'))
connect by level<=31
union
select (to_date(12||' '||:CAL_YEAR,'MM YYYY')-1 + level) DateRange,  
TO_CHAR(to_date(12||' '||:CAL_YEAR,'MM YYYY')-1 + level,'DAY') DAYNAME
from    dual
where   (to_date(12||' '||:CAL_YEAR,'MM YYYY')-1+level) <= last_day(to_date(12||' '||:CAL_YEAR,'MM YYYY'))
connect by level<=31)
;