Ask the Expert

Converting a column from VARCHAR2 to DATE

I need to convert a date column created using data type VARCHAR2 into data type DATE. Is it possible? How to do this? Thanks.

    Requires Free Membership to View

You cannot use the ALTER TABLE MODIFY command to change a VARCHAR2 column to a DATE column. However, you can do a multi-step process to get your data converted. Follow these steps:

  1. Add a column to your table to hold the DATE data.
       ALTER TABLE my_table ADD (new_col DATE);
    
  2. Set this new column's value to hold the old_column's value converted to a DATE value.
       UPDATE my_table SET new_col=TO_DATE(old_col,'MM/DD/YYYY');
    
    In this example, I used a format mask of MM/DD/YYYY assuming that my VARCHAR2 column (OLD_COL) contains the data in this format. You may have to use a different format mask.
  3. Drop the old column.
       ALTER TABLE my_table DROP (old_col);
    
  4. Rename the new column to be the old column's name.
       ALTER TABLE my_table RENAME new_col TO old_col;
    
    You may have to ensure that your application does not access the table while this type of reorganization is taking place.

This was first published in September 2005

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: