
.. _scalar:

================
Scalar Functions
================

date_truc
=========


.. _scalar-date-trunc:

date_trunc('interval', timestamp)
---------------------------------

The ``date_trunc`` function truncates given ``timestamp`` down to the given
``interval``.

Truncation is based on UTC time zone since Crate Data stores timestamps
as UTC milliseconds since the epoch.

.. seealso::

    :doc:`ddl`

Valid values for ``interval`` are:

* second

* minute

* hour

* day

* week

* month

* quarter

* year

The following example shows how to use the date_trunc function to generate a
day based histogram::

    cr> select date_trunc('day', date) as day, count(*) as num_locations
    ... from locations
    ... group by date_trunc('day', date)
    ... order by date_trunc('day', date)
    +---------------+---------------+
    | day           | num_locations |
    +---------------+---------------+
    | 308534400000  | 4             |
    | 1367366400000 | 1             |
    | 1373932800000 | 8             |
    +---------------+---------------+
    SELECT 3 rows in set (... sec)


.. Note::

    Currently scalar functions, such as ``date_trunc``, can only be used in
    the ``GROUP BY`` clause.
