Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Connect 2026! Turn data into bold moves, April 13 -15: Learn More!
cancel
Showing results for 
Search instead for 
Did you mean: 
paulwalker
Creator III
Creator III

search string in field

Hi Community,

I have below table - I have to compare Name and SearchName columns. (SearchName field has pipeline | separated text)

My scenario, I have to create flag based on compare between Name and SearchName. (IF Name field is available in SearchName field should flag 1 else 0)

below table is Flag is my output column

Name,  SearchName Flag
INDIA,  INDIA, 1
USA,  USA|Srilanka, 1
AUS,  INDIA, 0
PAK,  USA|Srilanka|NZ, 0
AUS,  INDIA|AUS|PAK, 1

 

Thanks in Advance!

Labels (1)
9 Replies
sergio0592
Specialist III
Specialist III

Hi,

You can achieve this whith creating three expressions :

Field1

if(SubField([SearchName Flag],'|',1)=Name,1,0)

 

Field2

if(SubField([SearchName Flag],'|',2)=Name,1,0)

 

Field3

if(SubField([SearchName Flag],'|',3)=Name,1,0)

 

And the final flag FL

rangesum(Field1,Field2,Field3)

 

sergio0592_0-1682428327648.png

 

 

 

 

 

paulwalker
Creator III
Creator III
Author

This should be create backend.

but this solutions is not correct I think, let say if I have 10 different text with delimiter  - I have to write 10 expressions.

Sabrina_V
Partner - Creator II
Partner - Creator II

Hello

try this : 

TEST:
Load * Inline [
Name,SearchName
INDIA,INDIA
USA,USA|Srilanka
AUS,INDIA
PAK,USA|Srilanka|NZ
AUS,INDIA|AUS|PAK
];

NoConcatenate
TMP:
Load
*
,SubStringCount (SearchName,Name) as Flag
Resident
TEST;

DROP TABLE TEST;

 

Here is the result : 

Sabrina_V_0-1682429102907.png

 

Qrishna
Master
Master

A small change to @Sabrina_V  code.

As substringcount returns the number times the substring exists in a field value,

 

TEST:
Load * Inline [
Name,SearchName
INDIA,INDIA
USA,USA|Srilanka
AUS,INDIA
PAK,USA|Srilanka|NZ
AUS,INDIA|AUS|PAK
];

NoConcatenate
TMP:
Load
*
, if(SubStringCount (SearchName,Name)<>0, 1, 0 )as Flag //tweak in the code here
Resident
TEST;

DROP TABLE TEST;

Kushal_Chawda

@paulwalker  simplest way will be below

Load *, if(index(SearchName,Name),1,0) as Flag
Inline [
Name,SearchName
INDIA,INDIA
USA,USA|Srilanka
AUS,INDIA
PAK,USA|Srilanka|NZ
AUS,INDIA|AUS|PAK
];

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

@Kushal_Chawda example is simple. But if there is any chance that you would have an incorrect partial match in your data, like name "IN", then a more robust approach that uses the delimiter might be:

Data:
Load *,
  RecNo() as RecId
Inline [
Name,SearchName
INDIA,INDIA
USA,USA|Srilanka
AUS,INDIA
PAK,USA|Srilanka|NZ
AUS,INDIA|AUS|PAK
];

D2:
Left Join (Data)
LOAD
  -Min(Name = AName) as Flag,
  RecId
Group By RecId
;
LOAD *, SubField(SearchName, '|') as AName
Resident Data
;

-Rob
http://www.easyqlik.com
http://masterssummit.com
http://qlikviewcookbook.com

Kushal_Chawda

@rwunderlich  indeed. 

MarcoWedel

Other methods to avoid false positives might be:

 

MarcoWedel_0-1682538195295.png

 

table1:
LOAD *,
     Sign(Index('|'&SearchName&'|','|'&Name&'|')) as Flag,
     -('|'&SearchName&'|' like '*|'&Name&'|*')    as Flag2
INLINE [
    Name, SearchName
    INDIA, INDIA
    USA, USA|Srilanka
    AUS, INDIA
    PAK, USA|Srilanka|NZ
    PAK, USA|Srilanka|PAK
    AUS, INDIA|AUS|PAK
    AUS, INDIA|AUSTRIA|NZ
    NZ,  INDIA|AUS|TANZANIA
    NZ,  INDIA|AUS|NZ
];


hope this helps

Marco

Chanty4u
MVP
MVP

May be try this 

 

LOAD 

    Name,

    SearchName,

    If(WildMatch(SearchName, '*' & Name & '*'), 1, 0) as Flag

FROM [your_data_source_here];