简体   繁体   中英

SQL: Convert Date to Null when Date is certian range from Today

I am working with an Oracle Datebase and I want to convert a date to null when it is more than a certain number of months from today.

The date format I am using is dd-mm-yy

So lets say you have this table:

WITH TEST_TABLE as (select '1' as num, '10-FEB-16' from dual
union(select '2', '10-FEB-30' from dual)
union(select '3', '10-FEB-50' from dual)
union(select '4', '10-FEB-17' from dual))

I know I can use: SELECT SYSDATE FROM DUAL; to get todays date but I am not sure how to compare this by months.

How would I change the dates to null when they are greater than say 24 months from today?

Something like this:

update test_table
    set dte = (case when add_months(dte, -12) < sysdate then dte end);

Note: You should be storing a date as a date data type in the database. The output format (by default) is the DD-MMM-YY format. But that is just the date formatted for output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM