- Lexicographic sorting: ULIDs can be sorted by their string representation, which corresponds to the order of their creation.
- Timestamp encoding: The first 48 bits of a ULID contain a millisecond-precision timestamp, allowing for easy extraction of creation time.
- Monotonicity: ULIDs generated in the same millisecond are guaranteed to be sortable and unique.
ClickHouse function reference
generateULID
Generates a ULID (Universally Unique Lexicographically Sortable Identifier). Syntax:x
(optional): An expression of any supported data type. The value is discarded, but the expression is used to bypass common subexpression elimination when the function is called multiple times in one query.
FixedString(26)
value representing the generated ULID.
Example:
Generate a single ULID:
1
and 2
ensure that each generateULID()
call produces a distinct value.
ULIDs are sortable and contain a timestamp, making them useful for generating time-ordered unique identifiers for database records, distributed systems, or any application requiring unique, time-sortable IDs.
ULIDStringToDateTime
Extracts the timestamp from a ULID (Universally Unique Lexicographically Sortable Identifier). Syntax:ulid
(String
orFixedString(26)
): The input ULID.timezone
(String
, optional): The timezone name for the returned value.
DateTime64(3)
]
Example:
taco_order_time
extracts the timestamp from a ULID representing a taco order.new_order_id
generates a new ULID for the next order.
The ULID contains a timestamp with millisecond precision, which this function extracts. The remaining bits in the ULID are random and not used by this function.