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).
views:
40answers:
3
+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
2010-07-02 07:07:46
I like this. I like this very much. Give me a few minutes to test it.
jake
2010-07-02 08:45:54
This will break one day a year on leap years. ADD_MONTHS is a more robust solution.
Allan
2010-07-02 17:30:59
+2
A:
1 year = 12 months, so subtract 12 months:
select add_months(sysdate,-12) from dual
Pop
2010-07-02 15:57:15
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
2010-07-02 19:01:25