+ Start a Discussion
nwingnwing 

What EXACTLY does the 'LIKE' statement mean?

 

I have used this in a query on the Attachment Object as a filter for the Name field.  I want it to filter in the following way.....

 

WHERE Name LIKE :CaseN AND Name LIKE 'ICB'

 

so it will pull anything with the Case Number and the string 'ICB' ANYWHERE in the Attachment Name.

 

As it seams to work I need to state the Name exactly to get any results...... which is no different then just using the ' = ' ......

 

Could anyone provide help on how to use the LIKE statement so it functions as a 'Contains' type of filter?

 

Thanks in advance....

 

 

 

		if(caseN != null){
			String filter1 = CaseN + '_ICB.xls';
			//String filter2 = CaseN;
			CurrentDoc =  [
			  SELECT Id, Name, ContentType, CreatedDate FROM Attachment WHERE Name LIKE :filter1
			];

 

PilkoTechPilkoTech

You need to add wildcards to your strings:

WHERE Name LIKE '%CaseN%' AND Name LIKE '%ICB%'.

 

The Underscore is an operator in SOQL, so you'll want to be careful using that.

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_select_comparisonoperators.htm

nwingnwing

 

ahh.... !  thanks big time for the link as well...... I was digging for that and just wasn't in the right spot....

 

 

nwingnwing

 

 

oohh...... one issue though.... 'CaseN' is a variable.  Any way to get that to work...... it doesn't appear to allow variables in a LIKE statement.......  am I wrong?

ahab1372ahab1372

try something like

 

 

LIKE '%' + variable + '%'

 

 

nwingnwing

 

I gave that a shot and the system did like the plus sign.....  I will try again to be sure...... but I don't think it took.....

ahab1372ahab1372

how inconvenient ...

then build your string first outside the query and use that as a variable. Something like

 

 

string querystring = '%' + CaseN + '%';
... Select ... WHERE Name LIKE :querystring;

 or, more specific to your code example, add the % to the filter1:

 

 

if(caseN != null){
    String filter1 = '%' + CaseN + '_ICB.xls'; // I think you don't need the trailing wildcard
 //String filter2 = CaseN; CurrentDoc = [SELECT Id, Name, ContentType, CreatedDate FROM Attachment WHERE Name LIKE :filter1];

 

 

 

 

nwingnwing

 

 

Oh, duh....... thanks.........

 

I did another way, but will print and post on my wall for future reminders......

 

Thanks for the help.  Always encouraging to get someone in a discussion on things.....

 

 

ahab1372ahab1372

nwing wrote:

 

I did another way, but will print and post on my wall for future reminders......

  


How did you do it?

nwingnwing

 

 

Oh, nothing fancy....... the way this is put together, I could just grab the parent id of the attachment directly...... probably a better way overall....... but I will definately be using the LIKE funcitonality more now that I have it in my head.

 

thanks again

BritishBoyinDCBritishBoyinDC

Remember you can also build the entire query as a string, and then use database.query to query for that string...sometimes easier when you are building dynamic SOQL...

 

 

List<sObject> L = Database.query(string);