views:

40

answers:

3

I have a database table containing credit card records. One of the fields is a Date field. I would like to update this field by changing the year portion of the date to 2011 if the year is less than 2010. From what i have found, PLSQL has functions for time and months but nothing to do with years (to my knowledge).

+3  A: 

This shows how to

with cc as(
select to_date('12-jan-1999') as cdate from dual union all
select to_date('12-jan-1921') as cdate from dual union all
select to_date('12-jan-1900') as cdate from dual union all
select to_date('12-jan-2000') as cdate from dual union all
select to_date('12-jan-2010') as cdate from dual
)
select  to_date( to_char(cdate,'DD-MON')  ||'-2011','DD-MON-YYYY')
from cc
where cdate < to_date('01-JAN-2010','DD-MON-YYYY')
/
josephj1989
I like this. I like this very much. Give me a few minutes to test it.
jake
This will break one day a year on leap years. ADD_MONTHS is a more robust solution.
Allan
+2  A: 

1 year = 12 months, so subtract 12 months:

select add_months(sysdate,-12) from dual
Pop
A: 

Here's how to do it so it works with leap years using add_months.

with cc as( 
select to_date('12-jan-1999','dd-mon-yyyy') as cdate from dual union all 
select to_date('12-jan-1921','dd-mon-yyyy') as cdate from dual union all 
select to_date('29-feb-1904','dd-mon-yyyy') as cdate from dual union all 
select to_date('12-jan-2000','dd-mon-yyyy') as cdate from dual union all 
select to_date('12-jan-2010','dd-mon-yyyy') as cdate from dual 
) 
select add_months(cdate,(2011 - extract( year from cdate)) * 12)  
from cc 
where cdate < to_date('01-JAN-2010','DD-MON-YYYY');
Daniel Emge