UUID and timeuuid

The database distributes data in a cluster based on the partition key. The uuid (universally unique id) type contains a unique value. Use this type as part of a partition key to uniquely identify a partition in a table.

Within a partition, the database orders rows by clustering columns. A timeuuid is a Type 1 UUID that includes the time of its generation. Order rows by time within a partition by using timeuuid values in clustering columns.

See the literal format for uuid and timeuuids in valid literals.

UUID function

You can use the UUID() function in a CQL statement to generates a random Type 4 UUID suitable for use in INSERT or SET statements. It takes no parameters.

Timeuuid functions

Several timeuuid() functions exist for use with the timeuuid type:

dateOf()

Used in a SELECT clause, this function extracts the timestamp of a timeuuid column in a result set. This function returns the extracted timestamp as a date. Use unixTimestampOf() to get a raw timestamp.

now()

In the coordinator node, generates a new unique timeuuid in milliseconds when the statement is executed. The timestamp portion of the timeuuid conforms to the UTC (Universal Time) standard. This method is useful for inserting values. The value returned by now() is guaranteed to be unique.

minTimeuuid() and maxTimeuuid()

Returns a UUID-like result given a conditional time component as an argument. For example:

SELECT a, b, toDate(c), toDate(d) FROM cycling.sample_times
  WHERE c > maxTimeuuid('2013-01-01 00:01+0000')
    AND c < minTimeuuid('2013-01-02 10:00+0000')
ALLOW FILTERING;

The preceding min/maxTimeuuid example selects all rows where the timeuuid column, t, is strictly later than 2013-01-01 00:01+0000 but strictly earlier than 2013-01-02 10:00+0000. The t >= maxTimeuuid('2013-01-01 00:01+0000') doesn’t select a timeuuid generated exactly at 2013-01-01 00:01+0000, and it is essentially equivalent to t > maxTimeuuid('2013-01-01 00:01+0000').

The values returned by minTimeuuid and maxTimeuuid functions aren’t true UUIDs because the values don’t conform to the Time-Based UUID generation process. The results of these functions are deterministic, unlike the now() function.

unixTimestampOf()

Used in a SELECT clause, this function extracts the timestamp in milliseconds of a timeuuid column in a result set. Returns the value as a raw, 64-bit integer timestamp.

Timestamp and timeuuid conversion functions

The following functions support some additional timeuuid and timestamp functions to manipulate dates. These functions can be used in INSERT, UPDATE, and SELECT statements:

toDate(timeuuid)

Converts timeuuid to date in YYYY-MM-DD format.

toTimestamp(timeuuid)

Converts timeuuid to timestamp format.

toUnixTimestamp(timeuuid)

Converts timeuuid to UNIX timestamp format.

toDate(timestamp)

Converts timestamp to date in YYYY-MM-DD format.

Example:

SELECT a, b, toDate(c), toDate(d) FROM cycling.sample_times
  WHERE c > maxTimeuuid('2013-01-01 00:01+0000')
    AND c < minTimeuuid('2013-01-02 10:00+0000')
ALLOW FILTERING;
toUnixTimestamp(timestamp)

Converts timestamp to UNIX timestamp format.

Examples:

INSERT INTO cycling.sample_times (
  a, b, c, d
) VALUES (
  1, toUnixTimestamp(now()), now(), toTimestamp(now())
);

INSERT INTO cycling.sample_times (
  a, b, c, d
) VALUES (
  2, toUnixTimestamp(now()), maxTimeuuid('2013-01-02 00:01+0000'), toTimestamp(now())
);

INSERT INTO cycling.sample_times (
  a, b, c, d
) VALUES (
  3, toUnixTimestamp(now()), maxTimeuuid('2013-01-03 00:03+0000'), toTimestamp(now())
);

INSERT INTO cycling.sample_times (
  a, b, c, d
) VALUES (
  4, toUnixTimestamp(now()), maxTimeuuid('2013-01-04 00:05+0000'), toTimestamp(now())
);
toTimestamp(date)

Converts date to timestamp format.

toUnixTimestamp(date)

Converts date to UNIX timestamp format.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2025 DataStax, an IBM Company | Privacy policy | Terms of use | Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com