<commit_before>
.. _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.<commit_msg>updated documentation<commit_after>
.. _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)


.. _scalar-date-trunc-tz:

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

As described above, by default the truncation is based on the UTC time zone.
``date_trunc`` accepts an optional time zone parameter
to override the default behaviour and truncate with the set ``timezone``.

Valid values for ``timezone`` are either the name of a time zone (for example
'Europe/Vienna') or the UTC offset of a time zone (for example '+01:00').
To get a complete overview of all possible values take a look at the
`available time zones`_ supported by `Joda-Time`_.

The following example shows the same day based histogram as above but truncated
with the ``timezone`` set to 'Europe/Moscow'::

    cr> select date_trunc('day', 'Europe/Moscow', date) as day, count(*) as num_locations
    ... from locations
    ... group by date_trunc('day', 'Europe/Moscow', date)
    ... order by date_trunc('day', 'Europe/Moscow', date)
    +---------------+---------------+
    | day           | num_locations |
    +---------------+---------------+
    | 308523600000  | 4             |
    | 1367352000000 | 1             |
    | 1373918400000 | 8             |
    +---------------+---------------+
    SELECT 3 rows in set (... sec)


.. Note::

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

.. _`available time zones`: http://www.joda.org/joda-time/timezones.html

.. _`Joda-Time`: http://www.joda.org/joda-time/
{'fraction_non_alphanumeric': 0.22193548387096773, 'fraction_numerical': 0.03354838709677419, 'mean_word_length': 1.954268292682927, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6675593', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1046, 'n_words': 377}