简体   繁体   中英

Oracle SQL TO_DATE - two date formats

I am trying to update several dates to the end of the month. However, my table has two separate date formats ('DD-MON-YY' and 'YYYYMMDD'). How can I update both dates in an update statement? Also, I want the new dates to be in 'YYYYMMDD' format.

Update MY_TABLE
set MY_DATE = TO_CHAR(LAST_DAY(TO_DATE(MY_DATE,'DD-MON-YY')),'YYYYMMDD');

As David noted in the comments, the "real" solution here would be to add a date column and use that. If that's not an option, you could differentiate old data from new data based on the existence of the - character:

UPDATE my_table
SET    my_date = 
       TO_CHAR(LAST_DAY(TO_DATE(my_date, CASE WHEN my_date LIKE '%-%' 
                                              THEN 'DD-MON-YY'
                                              ELSE 'YYYYMMDD' 
                                         END)),
                'YYYYMMDD')

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