Select to view content in your preferred language

Field Calculation Question

1702
6
Jump to solution
02-13-2013 07:57 AM
RyanHinesley
Occasional Contributor
I have a name field attributed with a string that has a hyphen in the middle...

'text1... - text2...'

I want to use the field calculator to calculate another field based on everything to the right of the hyphen, excluding the hyphen and everything to the left so the result will look like this...

'text2...'

Note that the number of characters for text1 and text2 is variable throughout the feature class so the RIGHT and LEFT functions will not work. Any ideas without having to revert to a python script?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
AnthonyGiles
Honored Contributor
Ryan,

Use an expression in the calculate field similar to :

Left of -

left([fieldname],instr([fieldname],"-")-1)

Right of -

Mid([fieldname],instr([fieldname],"-"),len([fieldname] - instr([fieldname],"-")

Or

Right([fieldname],len([fieldname] - instr([fieldname],"-"))


the instr function gives the position of a text string within another, and the left function extracts a string from another one, as does mid & right

Regards

Anthony

View solution in original post

0 Kudos
6 Replies
AnthonyGiles
Honored Contributor
Ryan,

Use an expression in the calculate field similar to :

Left of -

left([fieldname],instr([fieldname],"-")-1)

Right of -

Mid([fieldname],instr([fieldname],"-"),len([fieldname] - instr([fieldname],"-")

Or

Right([fieldname],len([fieldname] - instr([fieldname],"-"))


the instr function gives the position of a text string within another, and the left function extracts a string from another one, as does mid & right

Regards

Anthony
0 Kudos
RyanHinesley
Occasional Contributor
Worked perfectly. Thanks so much.

Ryan
0 Kudos
AnthonyGiles
Honored Contributor
Ryan,

Glad it worked, please don't forget to mark the post as answered,

Regards

Anthony
0 Kudos
StewartGalloway
Occasional Contributor

Anthony,

Can a version of your code help me to switch names within a field?  From "Smith, John" to "John Smith".

thanks.

Stew

0 Kudos
DanPatterson_Retired
MVP Emeritus

" ".join(a.split(", ").__reversed__())

where 'a' can be substituted with !YourFieldName!

use the python parser of course.

StewartGalloway
Occasional Contributor

wow, thanks Dan.

0 Kudos