ArcGIS-Calculate field : Give the first value to the others

I'm making a model builder to automatize some linear referencing projects but I have some problems with the field calculator tool.

My table contains 3 fields : ID | NOM | LIGNE and I'd like to complete the "LIGNE" field.

This row is numeric and it is composed by the 6 first number of the ID number 1.

I made a little picture to illustrate my problem :

(source: canardpc.com)
image

To resume, I'd like to complete my "LIGNE" field with the code "456789" which is the code line of the first ID.

Please... Could you help me ??

• Please accept the answer that solved your problem. To mark an answer as accepted, click on the check mark beside the answer to toggle it from hollow to green
– BERA
Oct 10 '16 at 17:12

Use the field calculator, select your field 'ligne' and try:

`Split( [NOM] , " ")(0)`

It splits the field (in your case "NOM") by whitespace characters (" ") and returns the first (index 0) segment.
This is a string operation, so the resulting data type is also a string. In case your result field has another type (e.g. integer), you have to convert it, like

``````CInt(Split( [input_field] , " ")(0))
``````

If the input value can be empty, you have to check first by using VBScript (in Pre-Logic Script Code), e.g.

``````value =  Split( [input_field] , " ")
If ubound(value) >= 0 Then
result = value(0)
Else
result = ""
End if
``````

and assign "result" to your field

• Hello ! Thank you for your help but it dosen't work. The result is good juste for the first ID and the others are "NULL". I have the same result when I made "left([NOM],6)". What I want to do is to input the first value of the "NOM" field to the others.
– bp03
Oct 10 '16 at 11:43
• @bp03: see the updated script, you have to put it in the "Pre-Logic Script Code" field, thanks to @fatih_dur! Oct 10 '16 at 12:21

Instead of Field Calculator you can use a python script and updatecursor. A python script can be can be integrated into models or executed inside ArcMap using the python window.

``````import arcpy
table = r'C:\TEST.gdb\Table123'
with arcpy.da.UpdateCursor(table,['NOM','LIGNE']) as ucurs:
for row in ucurs:
try:
index=str(row[0]).index(':')
value=int(row[0][:index])
row[1]=value
except ValueError:
row[1]=value
ucurs.updateRow(row)
``````

Try this (with an evident inspiration from @Greg Z) after setting your parser to `Python` and selecting `Show Codeblock` (inside `Pre-Logic Script Code`):

``````nom=None

def ligne_process(val):
global nom
val=long(val) if val.strip().isdigit() else False
if val and nom!=val:
nom=val
return nom
``````

And paste the following code in the smaller box below the Pre-Logic Script Code

``````ligne_process(!NOM!.split(":")[0])
``````

This code assumes, as @Greg Z highlighted, your target integer ID in NOM field is the first part just before the first colon (:) and your LIGNE field is an integer/numeric type.

EDIT

I have edited my answer as to respond your linear referencing related enquiry.