English

How To: Convert a string field to a numeric field

Summary

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.

Three solutions are described below; the first pertaining to ArcGIS Pro 1.x and 2.x; the second solution is for ArcGIS Desktop 10.x; and the third is for older software (such as ArcView GIS 3.x).

Procedure

For ArcGIS Pro 1.x and 2.x

  1. Open an ArcGIS Pro project, and add the applicable data or table to the project.
  2. Right-click the data or table in the Contents pane, and select Attribute Table.
  3. Click the Add button to create a new field.
  4. In the Fields list view, set the following properties:
    1. Set a name for the new field.
    2. Set the Data Type to a numeric type. Long or Double field types are most common.
    3. Set the Number Format to Numeric and adjust the settings as desired.
    4. If necessary, set the domain, default, and length values of the field.
  5. On the top ribbon (of the Fields tab), Save the edits to the table, and close the Fields view.
  6. Right-click the header of the newly created numeric field, and select Calculate Field. The Calculate Field geoprocessing dialog box opens.
  7. 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>!
An image of the Calculate Field pane.
  1. Click Run to run the tool.
Note:
The tool may fail to run if the string field contains Null (empty) values. Refer to the ArcGIS Pro web help document, Create a new field and apply a domain and default value for more information.

For ArcGIS Desktop 10.x

  1. Open ArcMap, and open the layer or file geodatabase attribute table.
  2. In ArcMap, the data type of the field can be checked by right-clicking the field's header and selecting Properties.
An image of the Field Properties dialog box.
  1. On the top-left of the Table window, click the Table Options drop-down button User-added image and select Add Field.
Note:
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.
  1. Provide a Name for the new field.
  2. Set the field Type to a numeric type, such as Long Integer or Double.
  3. Set the Precision and Scale if desired.
  4. Click OK.
  1. Right-click the newly created field's header and select Field Calculator.
  2. If the string field contains only numeric values, use the following steps to copy the attributes to the numeric field:
    1. Set the Parser to VB Script.
    2. Enter [String field name] into the expression window, or select the string field from the Fields box, and click OK.
An image of the Field Calculator dialog box.
  1. If the string field contains mixed alphanumeric and numeric values, use the following steps to copy the attributes:
    1. Set the Parser to VB Script, and check the check box for Show Codeblock.
    2. 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)
else
Output = null
End if
Note:
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)
  1. In the second expression window, (in this example, 'DoubleV ='), type:
Output
The image below shows how the Field Calculator dialog box looks. In this example, the string field is converted to a Double field.
A picture of the code entered into the Field Calculator window.
  1. Click OK. Once the script is complete, the numeric field is populated with the values from the string field.
Note:
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.

For ArcView GIS 3.x

To convert a string field to a number field, create a new number field, and copy the values from the original field to the new field.

Add a number field to the table
  1. Open the table and select Start Editing from the Table menu.
  2. Select Add Field from the Edit menu.
  3. In the Field Definition dialog box:
    1. Set Name to an appropriate name, for example 'Area'.
    2. Set Type to Number.
    3. Set Width to an appropriate number, for example '32'.
    4. Set Decimal Places to the desired number, for example '6'.
    5. Click OK.
  4. Select Stop Editing from the Table menu and save the edits.
Use Field Calculator to copy the values to the new field and convert the values to a number
  1. Open the table, and Start Editing if necessary.
  2. Select the Number field.
  3. Select Calculate from the Field menu.
  4. Double-click the string field from the Fields list.
  5. Set the Type to String.
  6. Scroll to the Requests list and double-click AsNumber.
  7. The final expression should look similar to following:
[Strng_fld].AsNumber
  1. Click OK.
  2. Select Stop Editing from the Table menu and save the edits.

Related Information