Page 1 of 1

Searching for a word in string in a column

Posted: Wed Sep 30, 2009 9:45 am
by Raamc
Hi,

I have a column with data COL1='APPLE BANANA CHERRY ETC'. I want to check weather the word BANANA is there in the COL1 or not?

word BANANA may come at any position in COL1. Currently it is 2nd word but in real data it may come as 5th word also, even in that case also i need to return a flag.

Can any one help? I need to do this in Server jobs.

Posted: Wed Sep 30, 2009 9:56 am
by ArndW

Code: Select all

INDEX(In.COL1,"BANANA",1)
will return 0 if the string is not found, or a numeric start position.

Re: Searching for a word in string in a column

Posted: Wed Sep 30, 2009 9:59 am
by robjones
Index(COL1,'BANANA',1) will return the position of the first occurrence of the text 'BANANA' in your column, or 0 if the text can't be found.

Posted: Wed Sep 30, 2009 10:00 am
by robjones
ArndW wrote:

Code: Select all

INDEX(In.COL1,"BANANA",1)
will return 0 if the string is not found, or a numeric start position. ...

Too quick for me!

Posted: Wed Sep 30, 2009 10:07 am
by Raamc
Hi All,

The word BANANA may come also as BANANALYKJKJS or BANANALY etc in COL1.
The below given code will return TRUE even for this kind of strings also?

Posted: Wed Sep 30, 2009 10:10 am
by ArndW
Yes, so look for "BANANA " and/or " BANANA" to avoid that type of error.

p.s. Rob - I'm here because my compiles are taking 30 minutes or longer today :)

Posted: Wed Sep 30, 2009 10:14 am
by Raamc
Thanks ArndW.

I will test this. Meanwhile i have given one try on function COUNT in basic. It is also working it seems.

I put this function in a routine and tested. Code is as below

Ans = COUNT (Arg1,Arg2)

Arg1 is my COL1
Arg2 is my string to search.

It is returning the valid count.

Thanks again,

Posted: Wed Sep 30, 2009 2:00 pm
by Sainath.Srinivasan
Better search with delimiters.

Code: Select all

sourceString = '|' : Convert(' ', '|', yourString) : '|'
searchString = '|' : yourSearchString : '|'
stringPresent = Index(sourceString, searchString, 1)
This will avoid BANANAMILKSHAKE to agree for BANANA.