In some scenarios, it is necessary to convert a field of string (text) values in an attribute table to numeric values, typically because the original data type was not set or recognized, or so that proper data analysis can be performed. To preserve data integrity (especially if sharing or importing data from outside sources), the method for converting fields to different types is to copy the data to a new field of the desired type.
Two solutions are described below; the first pertains to ArcGIS Pro 1.x and 2.x; the second solution is for ArcGIS Desktop 10.x (ArcMap).
For ArcGIS Pro 1.x and 2.x
- Open an ArcGIS Pro project, and add the applicable data or table to the project.
- Right-click the data or table in the Contents pane, and select Attribute Table.
- Click the Add button to create a new field.
- In the Fields list view, set the following properties:
- Set a name for the new field.
- Set the Data Type to a numeric type. Long or Double field types are most common.
- Set the Number Format to Numeric and adjust the settings as desired.
- If necessary, set the domain, default, and length values of the field.
- On the top ribbon (of the Fields tab), Save the edits to the table, and close the Fields view.
- Right-click the header of the newly created numeric field, and select Calculate Field. The Calculate Field geoprocessing dialog box opens.
- The Input Table, Field Name, and Expression Type are automatically filled. In the Expression Fields list window, locate and double-click the string field containing the attributes that must be transferred to the newly created numeric field. The following is entered in the Expression text box:
!<String field name>!
- Click Run to run the tool.
The tool may fail to run if the string field contains Null (empty) values. When creating the new numeric field, ensure the field is nullable to be able to import the Null values from the string field. Refer to the ArcGIS Pro web help document, Add field for more information.
For ArcGIS Desktop 10.x
- Open ArcMap, and open the layer or file geodatabase attribute table.
- In ArcMap, the data type of the field can be checked by right-clicking the field's header and selecting Properties.
- On the top-left of the Table window, click the Table Options drop-down button and select Add Field.
The Add Field option is only available when the user has write access to the data, and the data is not accessed by other users or applications. The option is also disabled when the map is in an edit session.
- Provide a Name for the new field.
- Set the field Type to a numeric type, such as Long Integer or Double.
- Set the Precision and Scale if desired.
- Click OK.
- Right-click the newly created field's header and select Field Calculator.
- If the string field contains only numeric values, use the following steps to copy the attributes to the numeric field:
- Set the Parser to VB Script.
- Enter [String field name] into the expression window, or select the string field from the Fields box, and click OK.
- If the string field contains mixed alphanumeric and numeric values, use the following steps to copy the attributes:
- Set the Parser to VB Script, and check the check box for Show Codeblock.
- Copy and paste the code below into the Pre-Logic Script Code text window, and replace 'String field name' with the name of the field to be converted. Leave the left and right brackets:
Dim String, Check
String = [String field name]
Check = IsNumeric(String)
If Check = True then
Output = CDbl(String)
Output = null
In the fifth line, replace 'CDbl(String)' to the following functions, according to the numeric field type selected in Step 3(b):
- Integer: CInt(String)
- Long: CLng(String)
- Double: CDbl(String)
- In the second expression window, (in this example, 'DoubleV ='), type:
The image below shows how the Field Calculator dialog box looks. In this example, the string field is converted to a Double field.
- Click OK. Once the script is complete, the numeric field is populated with the values from the string field.
If a string field contains values with non-numeric characters or symbols, the value of that cell remains <Null> by default when converting to the numeric field.