Changing Field Type with ArcPy in ArcGIS 10.0

arcgis-10.0arcpyfields-attributes

What I need to do is change data type of a field of a table or feature class. If there are some data, then I need to transfer it, if its possible, if not possible then just set null for that data. I need to do it using python script.

Problems:

  1. Because of before and after changing the data type, field name is same, I can not add new field with same name using arcpy.AddField_management method.
  2. Because of I need to transfer field values from first field to second one, I need to map both data types.

It will be good for me if I can find any ArcPy procedure similar to artwork21's answer to Renaming field using ArcPy?

Best Answer

There are essentially three options for making field schema changes in ArcGIS.

  1. Dropping the whole feature class/table and replacing it completely
  2. Dropping the field/column and replacing it -- this always puts the new column at the end, so not good if field order is important to you.
  3. Modifying column definitions in the underlying DBMS (dangerous, may cause corruption). This last one I wouldn't even go near unless you are in Oracle, and even then you have to be very careful (see this post).

It sounds like you want to go with option 2. The simplest way I can think of to do that is to add an intermediate/temporary column and calculate it from your original column. Then delete the original column, add a new column with the desired field type, calculate the new column from the intermediate column and finally delete the intermediate column.

Related Question