Partial comparison from the beginning of a character field
Sometimes you need to make a partial comparison that matches the beginning of a characters field, instead of matching the entire field. For example, you want to select a company named “Acme”, but you do not know whether the full name is “Acme Company”, “Acme, Inc.”, or even “Acme”.
In SQL, you apply a partial comparison predicate with the LIKE operator in the
WHERE clause. The following SQL statement applies the predicate
LIKE "Acme%" to values in the name column of the
companies table. The percent sign (%) is an SQL wildcard
symbol that matches zero or more characters.
SELECT * from companies
WHERE name LIKE "Acme%";
The query result contains companies with names that begin with “Acme”.
With the query builder APIs, you apply partial comparison predicates that match from the beginnings of character fields by using the startsWith method on the query object. Test the use of the startsWith method in a realistic environment. Using the startsWith method as the most restrictive predicate on a query can cause a delay on the user interface.
The following Gosu code applies the startsWith predicate method to
values in the Name field
on Company entity instances.
uses gw.api.database.Query
// Query the Company instances for a specific company.
var queryCompany = Query.make(Company)
queryCompany.startsWith(Company#Name, "Acme", false)
// Fetch the data with a for loop.
var result = queryCompany.select()
for (company in result) {
print (company.Name)
}
The sample code prints the names of companies that begin with “Acme”.
false as the third argument of
the startsWith method
specifies case-sensitive matching of values. In configurations that use
primary or secondary linguistic sort strength, the preceding query builder
code does not perform a case-sensitive comparison. If either your PolicyCenter application or the relational
database that your PolicyCenter
uses have these configurations, the result includes the names of companies
that begin with both “Acme” and “ACME.” See also
