Select to view content in your preferred language

Split a field if value exists at a specific index position. If not, return "0"

388
4
Jump to solution
07-23-2024 07:45 AM
gmaurer1123
Emerging Contributor

Hello all, 

I have a text field (i.e. DUE_TIME) and I need to split off the last value into a new field (i.e. VERSION). I have a script that works to this end, but I cannot incorporate it into modelbuilder (i.e. lack the skills currently) and as I am short on time, I have shelved that for now and am hoping to simply use the field calculator. The dilemma I'm having is that the field I'm trying to split has inconsistent indexing (i.e. month/day/year or month/day/year #) so trying to simply use the split function in the field calculator throws an error "index is out of range". Is there a way to get around this by using the code block in the field calculator? An If-Then Statement perhaps? My goal is to populate the VERSION field with the number after the date (in the DUE_TIME field) IF a number exists after the date, and otherwise, populate "0". Thank you all very much for your time and consideration. 

gmaurer1123_0-1721745894795.png

gmaurer1123_1-1721745915549.png

 

 

 

0 Kudos
3 Solutions

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

code block python parser, replace 'fld' with !YourFieldName!

def spl(fld):
    s = fld.split(" ")
    if len(s) > 1:
        return s[1]
    else:
        return 0
 
# examples       
fld = "2015/12/12"
spl(fld)
0

fld = "2015/12/12 2"

spl(fld)
'2'

If you need integers or whatever, 'int' the return portion


... sort of retired...

View solution in original post

gmaurer1123
Emerging Contributor

Dan, 

Thank you very much for the quick response, and for all of the answers you've provided in the ESRI community (you've kept me from losing my hair more than once). That being said, I'm still having trouble with this one.... I've applied your code in the code block, the expression validates, and I hit "apply", but still get an index error. Both the DUE_TIME and VERSION fields are text fields. I removed the exclamation points from the code block because they were throwing a syntax error. Am I missing something?

gmaurer1123_1-1721752363108.png

 

View solution in original post

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Try

spl(!DUE_TIME!)

in the  box where you currently have !DUE_TIME!.split(" ")[1]

and Dan's solution should work

View solution in original post

4 Replies
DanPatterson
MVP Esteemed Contributor

code block python parser, replace 'fld' with !YourFieldName!

def spl(fld):
    s = fld.split(" ")
    if len(s) > 1:
        return s[1]
    else:
        return 0
 
# examples       
fld = "2015/12/12"
spl(fld)
0

fld = "2015/12/12 2"

spl(fld)
'2'

If you need integers or whatever, 'int' the return portion


... sort of retired...
gmaurer1123
Emerging Contributor

Dan, 

Thank you very much for the quick response, and for all of the answers you've provided in the ESRI community (you've kept me from losing my hair more than once). That being said, I'm still having trouble with this one.... I've applied your code in the code block, the expression validates, and I hit "apply", but still get an index error. Both the DUE_TIME and VERSION fields are text fields. I removed the exclamation points from the code block because they were throwing a syntax error. Am I missing something?

gmaurer1123_1-1721752363108.png

 

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Try

spl(!DUE_TIME!)

in the  box where you currently have !DUE_TIME!.split(" ")[1]

and Dan's solution should work

gmaurer1123
Emerging Contributor

This worked perfectly! Thank you Dan! Thank you Alfred! 

0 Kudos