Skip to contentSkip to navigationSkip to topbar
On this page

Segmentation Query Language Reference


(warning)

Warning

The Segmentation v1 API was deprecated on December 31, 2022. Following deprecation, all segments created in the Marketing Campaigns user interface began using the Segmentation v2 API.

To enable manual migration and data retrieval, the GET and DELETE v1 API endpoints will remain available. The POST (create) and PATCH (update) v1 endpoints were removed on January 31, 2023 because it is no longer possible to create new v1 segments or modify existing ones. See our Segmentation v1 to v2 upgrade instructions to manually migrate your segments to the v2 API.


Data Types

data-types page anchor

Numeric

numeric page anchor

Any numeric type that can be an integer or float.

Integer: [1-9][0-9]*
Float: [0-9]+.[0-9]+

A set of characters delimited by double or single quotes.

Escaping

escaping page anchor

Escaping must be done for the character used as the delimiter if it is found within the string. The escape character is the backslash, \, which must also be escaped with a preceding \.

Escaping example
escaping-example page anchor
'Hello, World! It\'s a beautiful day'

When using the LIKE or NOT LIKE operators, The percentage symbol, %, will be interpreted as a wildcard character. To escape this character and not treat it as a wildcard, a second % should be used.

"email LIKE '%gmail.com'"

A timestamp whose literal value is formatted as a string in ISO 8601 format: YYYY-MM-DDTHH:mm:SSZ(-)HH:mm

A time interval with an integral scalar value and some unit of time, which can be one of the following: second, minute, hour, day, month, or year.

"10 day"

Boolean values are true or false.

Null is a special type that represents a lack of a value.


OperatorAssociativityOperands
ANDLeft2
ORLeft2
NOTRight2 (binary)
NOTRight1 (unary)

Precedence from low to high:

OperatorAssociativityOperandsSupported Types
-Left2 (binary)Numeric - Numeric DateTime - Interval
+Left2Numeric + Numeric DateTime + Interval String + String (concatenation)
/Left2Numeric / Numeric
*Left2Numeric * Numeric
%Left2Numeric % Numeric (modulo)
-Left1 (unary)- Numeric
OperatorSupported Types (T represents any type)
=T = T
!=T != T
<Numeric < Numeric DateTime < DateTime String < String
>Numeric < Numeric DateTime < DateTime String < String
&lt;=Numeric < Numeric DateTime < DateTime String < String
>=Numeric < Numeric DateTime < DateTime String < String
LIKE/ NOT LIKEString (NOT) LIKE String
IS (NOT)T is (NOT) NULL
(NOT) INT IN (T)
(NOT) BETWEENNumeric (NOT) BETWEEN Numeric AND Numeric DateTime (NOT) BETWEEN DateTime AND DateTime String (NOT) BETWEEN String AND String

Identifiers are named things within a given query. These include both function names and field/column names. Identifiers cannot be a keyword and must only allow the characters: [a-zA-Z_]+..

(information)

Info

Identifiers that do not meet the previous format may still be used. However, they must be encapsulated within backticks. I.E. 000supercoolid


Functions can be invoked with or without parameters by providing the function name—remember, function names are identifiers—followed by a list of comma separated arguments enclosed in parentheses.

Function called with arguments example

function-called-with-arguments-example page anchor
MY_FUNCTION(a,b,c)

These are functions that should be used consistently across consumers of the parser. Whether or not your implementation actually supports them is up to you.

CONTAINS(array_or_map, value_or_key)

Contains should return a Boolean indicating the presence of a value in an array or map. When used with an array, true should be returned when the array holds the given value. When used with a map, true should be returned when the map has an element with the given key.

CONCAT(string_one,string_two)

Concat takes two strings, combines them as a single string in the order they are passed in, and returns the result.

LENGTH(string)

Length takes a single string and returns the number of characters in the string.

LOWER(string)

Lower returns a lowercase version of the given string.

NOW()

Returns the current date and time.


A number of fields are available on every contact. These include the strings:

  • alternate_emails
  • address_line_1
  • address_line_2
  • city
  • contact_id
  • country
  • created_at
  • email
  • phone_number_id
  • external_id
  • anonymous_id
  • email_domains
  • event_data
  • event_source
  • event_timestamp
  • event_type
  • first_name
  • list_ids
  • last_name
  • postal_code
  • state_province_region
  • updated_at
(information)

Info

In the future, the address fields may be used with a third-party service to populate a location type field when contacts are added or updated. In addition, a contact's alternate_emails field represents a set of strings.


A query for getting all gmail users

a-query-for-getting-all-gmail-users page anchor
1
{
2
"name": "All Gmail Users",
3
"query_dsl": "email LIKE '%gmail.com'"
4
}

A query for getting contacts in specific zip codes

a-query-for-getting-contacts-in-specific-zip-codes page anchor
1
{
2
"name": "My Favorite Zip Codes",
3
"query_dsl": "postal_code IN ('90124', '90125', '90126')"
4
}

A query for getting contacts NOT in specific zip codes

a-query-for-getting-contacts-not-in-specific-zip-codes page anchor
1
{
2
"name": "My Least Favorite Zip Codes",
3
"query_dsl": "postal_code NOT IN ('90124', '90125', '90126')"
4
}

A query showing how to use lowercase text

a-query-showing-how-to-use-lowercase-text page anchor
1
{
2
"name": "Everyone named Bob, BOB or bob",
3
"query_dsl": "lower(first_name) = 'bob'"
4
}

A query for contacts that received any email in the last 10 days

a-query-for-contacts-that-received-any-email-in-the-last-10-days page anchor
1
{
2
"name": "All Delivered in Last 10 days",
3
"query_dsl": "(event_source = 'mail' AND event_type = 'delivered' AND event_timestamp >= (NOW() - interval 10 day))"
4
}

A query for contacts that received any email between two dates

a-query-for-contacts-that-received-any-email-between-two-dates page anchor
1
{
2
"name": "All Delivered in Last 10 days",
3
"query_dsl": "(event_source = 'mail' AND event_type = 'delivered' AND event_timestamp BETWEEN TIMESTAMP '2019-08-07T18:00:00Z' AND TIMESTAMP '2019-08-08T18:00:00Z')"
4
}

A query for getting contacts by external_id

a-query-for-getting-contacts-by-external_id page anchor
1
{
2
"name": "My External ID Contacts",
3
"query_dsl": "external_id LIKE '123%'"
4
}

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.