Two OData query features cause a disproportionate share of confusion: $count, because V2 and V4 spell it differently, and the $filter string functions, because substringof still shows up in old code and Stack Overflow answers. Get these two right and your queries become both correct and portable.
$count: knowing how many before you fetch
When a UI shows "Showing 20 of 1,347 orders," the 1,347 comes from a count. In OData V4 you ask for it with $count=true:
GET /odata/v4/sales/Orders?$filter=status eq 'OPEN'&$count=true&$top=20
The response includes the total alongside the page of results:
{
"@odata.count": 1347,
"value": [
{ "orderId": "450001", "status": "OPEN", ... },
...
]
}
Note the details: @odata.count reflects the count after $filter but before $top/$skip. That is exactly what pagination needs — the total matching set, not the page size.
You can also request just the number, without any entities:
GET /odata/v4/sales/Orders/$count?$filter=status eq 'OPEN'
→ 1347 (plain text response)
The V2 equivalent was $inlinecount=allpages, which returned __count in the payload. If you are migrating V2 code, this is a straight mechanical replacement — but watch out for any client code that reads the old __count property name.
$count=true gives you the filtered total with your page of data. /$count gives you just the number. Both count after $filter, before $top/$skip.
$filter functions: contains() is the V4 way
Filtering on exact matches (status eq 'OPEN') is straightforward. Substring search is where versions diverge. In V4, the canonical function is contains:
GET /odata/v4/sales/Customers?$filter=contains(name, 'Acme')
In V2, the equivalent was substringof — with the arguments reversed:
GET /odata/v2/sales/Customers?$filter=substringof('Acme', name) eq true
That reversed argument order is a classic migration trap. V4's contains(property, 'text') reads naturally: does this property contain this text? V2's version reads backwards and returns an explicit boolean you had to compare with eq true.
Other string functions you will use constantly in V4 filters:
$filter=startswith(orderId, '450') → prefix match
$filter=endswith(email, '@example.com') → suffix match
$filter=contains(tolower(name), 'acme') → case-insensitive search
$filter=length(description) gt 100 → length check
The tolower trick deserves emphasis: contains is case-sensitive, so wrapping the property in tolower (and lowercasing your search term) is the standard way to do case-insensitive search. Whether the backend translates this efficiently depends on the service — on HANA-backed CAP services it generally becomes a proper SQL predicate.
Combining filters
Real filters combine conditions with and, or, and not. Parentheses control precedence, and the rules are the same as in most languages — and binds tighter than or:
$filter=status eq 'OPEN' and (contains(name, 'Acme') or total gt 1000)
A few practical rules of thumb:
- Filter before you count, count before you page.
$filter+$count=true+$top/$skipis the standard pagination recipe. - Prefer server-side filtering over client-side. Fetching 10,000 entities to filter in the browser is never the answer — if a filter is not supported, that is a service gap to fix, not a client workaround to build.
- Check $metadata for filterable properties. Services can mark properties as non-filterable. The metadata tells you; guessing does not.
When migrating V2 queries: $inlinecount=allpages becomes $count=true, and substringof('x', prop) becomes contains(prop, 'x'). The argument order flips — that is the detail that breaks migrations.
Nulls, dates, and the in operator
Three more filter details that come up in real queries. First, null checks use plain comparison — no is null keyword:
$filter=shippedAt eq null → not yet shipped
$filter=customerId ne null → has a customer assigned
Second, date and time parts have their own functions, which is far cleaner than string-slicing an ISO timestamp:
$filter=year(orderDate) eq 2026
$filter=month(orderDate) eq 9 and day(orderDate) ge 15
Third, V4.01 added the in operator, which replaces long chains of or conditions:
" instead of this:
$filter=status eq 'OPEN' or status eq 'PENDING' or status eq 'BACKORDER'
" write this:
$filter=status in ('OPEN', 'PENDING', 'BACKORDER')
Support for in depends on the service version — CAP-based V4 services generally handle it, older Gateway V4 stacks may not. If it fails, fall back to the or chain; the semantics are identical.
Counting at scale: a performance note
$count=true is convenient, but on very large entity sets the count itself can be expensive — the database still has to evaluate the full filtered set to count it. A few guidelines from the field:
Count once, page many. The total only changes when the underlying data or the filter changes. Cache @odata.count client-side for the session rather than requesting it on every page turn.
Watch unfiltered counts on huge sets. GET /Orders/$count with no filter on a table with tens of millions of rows is a full table scan. If your UI only needs "more than 10,000," consider whether the service offers a capped count instead.
Let the database do the work. A filter the backend can translate into a SQL predicate (equality, ranges, contains on indexed columns) keeps both the count and the page fast. A filter on a computed or unmapped property may force the service to materialize rows first — check $metadata for which properties are actually filterable and sortable.
Pagination is a contract between client and service: $filter narrows, $count=true totals, $top/$skip pages. Put all three in one request and the round trip stays constant no matter how deep into the list the user scrolls.
Quick reference
| Need | V4 | V2 (for migration) |
|---|---|---|
| Total with results | $count=true → @odata.count | $inlinecount=allpages → __count |
| Just the number | /EntitySet/$count | /EntitySet/$count |
| Substring match | contains(prop,'x') | substringof('x',prop) eq true |
| Prefix match | startswith(prop,'x') | startswith(prop,'x') eq true |
Bottom line
$count=true plus $filter plus $top/$skip is the complete pagination recipe — one request returns a page of data and the total it was drawn from. For text search, contains() is the V4 function to reach for, with tolower() as the standard case-insensitivity trick. And when you migrate old V2 queries, watch the two traps: the __count property name disappears, and substringof's arguments flip order. Get those right and your queries work first time on any V4 service.