Select to view content in your preferred language

Attribute Table- Field calculation using conditional IF

657
5
01-04-2012 05:09 AM
BabakMirshahi
Emerging Contributor
Hi,

To calculate the values in Field: "Start_time" using those in Field: "Period_Sta", I tried expression below. However it doesn't work. Can anybody help to debug it?
Thanks

If [Period_Sta] =="01/01" Then
[Start_time]= ???1???
elseif  [Period_Sta] == "01/04" Then
[Start_time]= ???91???
elseif  [Period_Sta] == "30/09" Then
[Start_time]= ???273???
elseif  [Period_Sta] == "31/12" Then
[Start_time]= ???365???
end if
0 Kudos
5 Replies
BruceNielsen
Frequent Contributor
If [Start_time] is defined as a numeric field, then you don't need the quotes around the value ([Start_time] = 1).
0 Kudos
BabakMirshahi
Emerging Contributor
[Period_Sta] is a string field & [Start_time] is defined as Double. Even after removing the "", it sends the same error message: "There was a failure during processing".
0 Kudos
BabakMirshahi
Emerging Contributor
If [Start_time] is defined as a numeric field, then you don't need the quotes around the value ([Start_time] = 1).




[Period_Sta] is a string field & [Start_time] is defined as Double. Even after removing the "", it sends the same error message: "There was a failure during processing".
0 Kudos
ChrisSnyder
Honored Contributor
An update cursor in Python would be much easier to format this, but if you wanted to do it in the field calculator, the Python formatted code would look like this ugly thing here:

gp.CalculateField_management(myFC, "Start_time", "returnvalue(!Period_Sta!)", "PYTHON", "def returnvalue(periodStaFieldValue):\\n if periodStaFieldValue == '01/01':\\n   return '1'\\n elif periodStaFieldValue == "01/04":\\n   return '91'\\n elif periodStaFieldValue == "30/09":\\n   return '273'   \\n elif periodStaFieldValue == "31/12":\\n   return '365'")


or in an update cursor...

updateRows = arcpy.UpdateCursor(myFC)
for updateRow in updateRows:
   if updateRow.Period_Sta ==  '01/01':
      updateRow.Start_time = '1'
   elif updateRow.Period_Sta ==  '01/04':
      updateRow.Start_time = '91'
   elif updateRow.Period_Sta ==  '30/09':
      updateRow.Start_time = '273'
   elif updateRow.Period_Sta ==  '31/12':
      updateRow.Start_time = '365'
   updateRows.updateRow(updateRow)
del updateRow, updateRows
0 Kudos
RichardFairhurst
MVP Honored Contributor
Hi,

To calculate the values in Field: "Start_time" using those in Field: "Period_Sta", I tried expression below. However it doesn't work. Can anybody help to debug it?
Thanks

If [Period_Sta] =="01/01" Then
[Start_time]= �??1�?�
elseif  [Period_Sta] == "01/04" Then
[Start_time]= �??91�?�
elseif  [Period_Sta] == "30/09" Then
[Start_time]= �??273�?�
elseif  [Period_Sta] == "31/12" Then
[Start_time]= �??365�?�
end if


Try adding an unqualified Else condition at the end that calculates [Start_time] = [Start_time] if none of the other conditions are met.  Also, if [Period_Sta] has Null values you either need to select non-Null values first or else make the first condition If IsNull([Period_Sta]) Then [Start_time] = [Start_time]
0 Kudos