尝试从日期提取月份时收到错误

时间:2012-12-03 12:09:08

标签: sql oracle11g

我想从输入日期中提取月份。我正在尝试这个

select
    case
        when extract(month from :myDate) <= 6 then '1st' 
        when extract(month from :myDate) >= 7 then '2nd'
    end as half_of_the_year
from dual

如果我输入2-12-12,我会收到错误

ORA-30076: invalid extract field for extract source
30076. 00000 -  "invalid extract field for extract source"
*Cause:    The extract source does not contain the specified extract field.
*Action:

为什么我收到此错误?

由于

1 个答案:

答案 0 :(得分:2)

尝试使用to_date函数将绑定变量显式转换为date数据类型:

select
    case
        when extract(month from to_date(:mydate, 'dd-mm-rr')) <= 6 then '1st'
        when extract(month from to_date(:mydate, 'dd-mm-rr')) >= 7 then '2nd'
    end as half_of_the_year
from dual