function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ramlakhanramlakhan 

How to query over a public group whose 4th letter of name is '_'

I tried indexof and charat too.But its not working.I  want to pass this query to Database.querylocator obejct of batch class.Thanks in advance!
Best Answer chosen by ramlakhan
Shingo YamazakiShingo Yamazaki
Hi, ramlakhan

I'm Shingo.

How about this?

return Database.getQueryLocator(String.join(new List<String> {
	'SELECT',
		'Name',
	'FROM',
		'Group',
	'WHERE',
		'Name LIKE \'___\\_%\''
}, ' '));


All Answers

Shingo YamazakiShingo Yamazaki
Hi, ramlakhan

I'm Shingo.

How about this?

return Database.getQueryLocator(String.join(new List<String> {
	'SELECT',
		'Name',
	'FROM',
		'Group',
	'WHERE',
		'Name LIKE \'___\\_%\''
}, ' '));


This was selected as the best answer
ramlakhanramlakhan
Hi Shingo,thanks! Its now working perfectly.
ramlakhanramlakhan
Hi Shingo, But  the query :SELECT name from group where Name LIKE '__\_%' is not retriving  anything when i am trying to check it in execute anonymous ,though it is retriving expected records while fired on query editor.Seems very strange!!.
In execute anon I am putting query as:
List<group> li=new List<group>([SELECT name from group  WHERE Name LIKE '__\_%']);
this gives empty list.

Shingo YamazakiShingo Yamazaki
Though I don't know the reason, but you need to use bind variable.

This works fine.
String key = '___\\_%';
List<Group> groups = [SELECT Name From Group WHERE Name LIKE :key];
More strangely, this query [select id, name from group where name like '___\_%'] works in Query Editor...

ramlakhanramlakhan
Thanks Shingo .Its working now.