Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract First Word

Status
Not open for further replies.

atgrovecity

Programmer
Joined
Jul 18, 2003
Messages
4
Location
US
Hello,

I need the code to extract the first word in a string. For some records, the the generic drug name name will only have one word, sometimes more than one word. In both scenarios I need to the code to extract the first word.

Example:

OLANZAPINE
OLANZAPINE PAMOATE

The output of both records should be just OLANZAPINE

Thanks.

Joe
 
Try something like this:

NumberVar i;
i := InStr({myTable.MyField}), " ");
if i > 0 then substring({myTable.MyField}, 1, i - 1) else {myTable.MyField}

-Dell

DecisionFirst Technologies - Six-time SAP BusinessObjects Solution Partner of the Year
 
or


split({myTable.MyField}," ")[1]

_____________________________________
Crystal Reports 2008 and XI
Intersystems Cache 2012 ODBC connection

 
CoSpringsGuy - you'll want to have a control in case there is no second word in the string, something like this:

if instr({myTable.MyField}," ")<1 then {myTable.MyField} else split({myTable.MyField}," ")[1]

-jcrawford-

Not in word only, but in deed also... 1Jn3:18
 
jcrawford08 - Why do you need control? My solution works with one word or a thousand words .. try it

_____________________________________
Crystal Reports 2008 and XI
Intersystems Cache 2012 ODBC connection

 
What you have been thinking was if the field is empty in which case

if len({@text1}) > 0 then split({@text1}," ")[1]

will work....


_____________________________________
Crystal Reports 2008 and XI
Intersystems Cache 2012 ODBC connection

 
My apologies, I guess that's a habit I've gotten in. I seemed to recall coming into some issues previously where without a control the split formula would error out, but the specifics must have been different than they are in this situation.

-jcrawford-

Not in word only, but in deed also... 1Jn3:18
 
Thank you for vindicating that I have not lost my mind completely :)

-jcrawford-

Not in word only, but in deed also... 1Jn3:18
 
No probs! I have learned A LOT through this forum and mainly through discussions like this one...

_____________________________________
Crystal Reports 2008 and XI
Intersystems Cache 2012 ODBC connection

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top