Comparisons and Logical Operators

Introduction

Starcounter implements operators that can be used for filtering in WHERE clauses. They can, for example, check if a value is equal to another value or if a value matches a specific pattern.

Relational Operators

Equality

The comparison predicates "equal" (x = y) and "not equal" (x <> y) are supported for all data types. See for example query below.
1
SELECT e FROM Employee e WHERE e.FirstName = 'Bob'
Copied!
Starcounter SQL is case insensitive, so "Bob" will also match with "bob", "BOB" and so on.

Comparisons

The comparison predicates "less than" (x < y), "greater than" (x > y), "less than or equal" (x <= y) and "greater than or equal" (x >= y) are implemented for the data types String, DateTime and all numerical types. See example below.
1
SELECT e FROM Employee e WHERE e.LastName >= 'Smith'
Copied!
Since a DateTime value represents a timestamp it is often necessary to compare it with a DateTime range. The query below returns all employees with a HireDate between '2006-11-01 00:00:00.000' and '2006-11-01 23:59:59.999'.
1
SELECT e.FirstName, e.HireDate FROM Employee e
2
WHERE e.HireDate >= DATE '2006-11-01'
3
AND e.HireDate < DATE '2006-11-02'
Copied!

Logical Operators

There are three logical operators in Starcounter SQL, AND, OR, and NOT:
1
SELECT e FROM Employee e
2
WHERE e.FirstName = 'Bob'
3
AND e.LastName = 'Smith'
4
5
SELECT e FROM Employee e
6
WHERE e.FirstName = 'Bob'
7
OR e.FirstName = 'John'
8
9
SELECT e FROM Employee e
10
WHERE NOT e.FirstName = 'Bob'
Copied!
The logical operators NOT and OR usually imply that indexes can't be used in the execution of the query, and therefore these operators should be used restrictively.

IS NULL

The comparison predicates "is null" (x IS NULL) and "is not null" (x IS NOT NULL) are implemented for all data types. See for example query below.
1
SELECT e FROM Employee e WHERE e.Manager IS NULL
Copied!

LIKE

The comparison predicate "like" (x LIKE y [ESCAPE z]) is implemented for the data type String. In the specified pattern (y) the underscore character ('_') match any single character in the string, and the percent character ('%') match any sequence (possibly empty) of characters in the string. See for example query below.
1
SELECT e FROM Employee e WHERE e.FirstName LIKE 'B_b%'
Copied!
The optional third argument to the LIKE predicate is an "escape character", for use when a percent or underscore character is required in the pattern without its special meaning. This is exemplified in query below.
1
SELECT s FROM Share s WHERE s.Unit LIKE '\%' ESCAPE '\\'
Copied!

IS

The comparison predicate IS checks if an object can be cast to a given type. It has similar semantics to Type.IsAssignableFrom. Consider, for example, the following code:
1
[Database]
2
public class Person
3
{
4
public string FirstName { get; set; }
5
}
6
7
[Database]
8
public class Teenager : Person
9
{
10
}
11
12
[Database]
13
public class Child : Teenager
14
{
15
}
16
17
class Program
18
{
19
static void Main()
20
{
21
Db.Transact(() =>
22
{
23
new Person { FirstName = "Bob" };
24
new Teenager { FirstName = "Johnny" };
25
new Child { FirstName = "Elsa" };
26
});
27
}
28
}
Copied!
Here, the IS operator can be used to determine inheritance in the data model. For instance, to retrieve all instances that can be cast to Teenager, a query like this would be used:
1
SELECT p FROM Person p WHERE p IS Teenager
Copied!
It returns the Child and Teenager instances "Johnny" and "Elsa" because they can both be cast as Teenager.
Last modified 4yr ago