简体   繁体   中英

sql oracle update date

My table table1 has the column date_txt which includes 2/16/2011 12:00:00 AM - column date_txt is VARCHAR2 (250 Char) .

My table table1 also has the column date which is a DATE .

I would like to "update" my field:

The final output should be:

table1:

 date

 2/16/2011

So it takes from table1 date_txt the "date" and updates it to the column date as a date.

Any ideas? I am a bloody beginner.

You can use Oracle's to_date function to convert a string to a date:

update table1 set "date" = to_date(date_txt, 'MM/DD/YYYY HH:MI:ss AM')

See it working at SQL Fiddle.

column date_txt which includes 2/16/2011 12:00:00 AM

  • Firstly, DATE doesn't have any format . What you see is for display purpose to interpret date value easily.

  • Secondly, you should never ever store DATE as VARCHAR2 . This is a big problem and a poor design.

Now that you have a bad design, it is a good idea to fix it right now.

Follow these steps:

  1. Add a new column with DATE data type .
  2. Update the new column with date values from the old column using TO_DATE .
  3. Drop the old column.
  4. Rename the new column to the old column.

I have already answered how to do it here https://stackoverflow.com/a/29625772/3989608

Try following:

update table1 set date=
    (select to_date(date_txt,'dd/mm/yyyy') from table1 where id=yourId) where id=yourId;

Use primary key columns for ids.

If this is what you need everytime. I would suggest you to use Triggers

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